Skip to content
Advertisement

How to load parameters with different values based on user role in Symfony?

I have a multitude of parameters in parameters.yml file, each parameter should have a different value based on logged in user’s role.

Is there is way to systematically decide and load correct value based on logged in user’s role?

Example:

JavaScript

Ideally, I don’t want to add conditions in my code for manager or user before I use address_code.

Advertisement

Answer

Since parameters values are used during the container compilation, their value cannot be changed at runtime.

And since the user role can only be known during a request, what you want to do simply cannot be done.

But you are most likely you are approaching the problem from the wrong end.

Just encapsulate the logic in a service of some kind. A simplistic example:

The service:

JavaScript

Your parameters:

JavaScript

Configuration to make sure the service know about your parameters:

JavaScript

Then it’s only a matter of injecting FooService wherever you need these values, and call getAddressCodeFor($role) where you need it:

JavaScript

This is just an example, but you can adjust it to follow your needs.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement