Pierre de LESPINAY Pierre de LESPINAY - 2 months ago 6x
YAML Question

How to access an application parameters from a service?

From my controllers, I access the application parameters (those in

) with


But I don't know how to access it from a service (I imagine my service class is not supposed to extend

Should I map needed parameters into my service registration like this:

my_param1: %my_param1%
my_param2: %my_param2%
my_param3: %my_param3%

or something similar? How should I access to my application parameters from a service?


You can pass parameters to your service in the same way as you inject other services, by specifying them in your service definition. For example, in YAML:

        class:  My\Bundle\Service\MyService
        arguments: [%param_name_1%, %param_name_2%]

where the %param_name_1% etc corresponds to a parameter named param_name_1. Then your service class constructor could then be:

public function __construct($paramName1, $paramName2)
    // ...