Symfony Framework: Using methods and constants in service parameters

KC Müller
1 min readJun 11, 2018

--

Here is a tutorial on how to access methods and constants of other service classes in your services.yaml file. The example can be adapted to XML/PHP syntax as described in the official documentation.

You will need the Symfony Expression Language component so that the syntax is recognized. On your console run:

composer require symfony/expression-language

The following example is showing the syntax:

The first argument is accessing the “getCustomString” method of the “My\Custom\Config” class. Be sure to use four backslashes in the namespace for escaping reasons. You can access any class that is registered as a service.

The second argument is getting the constant “MY_CONSTANT” from the “ServiceConstants” class using the “!php/const” annotation that was introduced in symfony 3.2. The expression language is not needed here.

The third argument is accessing a parameter from the service container.
Note: I changed this example from getting the user id from the token storage here because this wasn't a clean approach.

Read more on the expression language syntax in the official documentation.

--

--