Accessing configuration parameters in Symfony 2

Hi,

Today, in this small post, I am going to show you how to access a parameter which is defined in config.yml file, inside the program.

Imaging you are creating/have a parameter called ‘is_something_enabled‘  inside config.yml. You need to access this parameter inside your code, let’s say inside a controller. This is how you do this. First of all let’s see how the parameter is defined in config.yml file.

parameters:
  is_something_enabled: true
Accessing this in a controller is very easy. Actually you don’t need to be in a controller to access these parameters. What you need is the container. If you can inject the container, to any of your class, that’s pretty much it. Here is how you access the above parameter in a controller,
$this->container->getParameter(‘is_something_enabled’);
In a container injected constructor class, this is how you access it. (Refer to my previous post to see how to inject the container to constructor)
Share