Skip to content

Instantly share code, notes, and snippets.

@KaiCMueller
Last active June 18, 2018 12:36
Show Gist options
  • Save KaiCMueller/dc74e2bfbe40db5cd8ad213a1b923d17 to your computer and use it in GitHub Desktop.
Save KaiCMueller/dc74e2bfbe40db5cd8ad213a1b923d17 to your computer and use it in GitHub Desktop.
Symfony Framework: Using methods and constants in service parameters
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'"
@KaiCMueller
Copy link
Author

KaiCMueller commented Jun 11, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment