Accessing global parameters from route condition expressions in Symfony

I am trying to access symfony application parameters (defined in app / config / parameters.yml) from an expression in route state ( documentation ).

I tried my luck by inserting the parameter in percentage signs and through the "parameter" function (as described for DI here ), both of which didn't help.

Here's an example with a parameter function:

example_route:
    path:     /example/{_locale}
    condition: "request.getLocale() in parameter('locales_array')"
    defaults: 
        _controller: "AcmeExampleBundle:Example:index"
        _locale: %locales_default%

      


However, I get:

SyntaxError . The parameter function does not exist at position 24.


Is there a way to access parameters from routing condition expressions?

+3


source to share


2 answers


I opened a PR at https://github.com/symfony/symfony/pull/12869 for version 2.7 until it gets executed and shipped you will have to use your own extended version if UrlMatcher is in your project where you need will add the container function provider and also add the container to the "variables" argument of the ExpressionLanguage # so you can access the parameters and services.



You can look at the PR for a hint on how to do this, I'll write about it in more detail if you need it later.

+3


source


Lucky for you (and I'm a little late), I access Symfony parameters from an expression using the% variable% syntax.

Here's your example, tested in Symfony 3:



example_route:
    path:     /example/{_locale}
    condition: "request.getLocale() in ['%locales_array%']"
    defaults: 
        _controller: "AcmeExampleBundle:Example:index"
        _locale: %locales_default%

      

+1


source







All Articles