Last active
June 18, 2018 12:36
-
-
Save KaiCMueller/dc74e2bfbe40db5cd8ad213a1b923d17 to your computer and use it in GitHub Desktop.
Symfony Framework: Using methods and constants in service parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
My\Custom\Service: | |
class: My\Custom\Service | |
arguments: | |
- "@=service('My\\\\Custom\\\\Config').getCustomString()" | |
- !php/const My\Custom\ServiceConstants::MY_CONSTANT | |
- "@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Read more on the expression language syntax in the official documentation.