问题
I'm trying to access app-wide symfony parameters (defined in app/config/parameters.yml) from an expression in a route condition (documentation).
I tried my luck inserting the parameter within percentage signs and through the function "parameter" (as described for DI here), both to no avail.
Here is the example with the parameter function:
example_route:
path: /example/{_locale}
condition: "request.getLocale() in parameter('locales_array')"
defaults:
_controller: "AcmeExampleBundle:Example:index"
_locale: %locales_default%
However I'm getting:
SyntaxError - The function "parameter" does not exist around position 24.
Is there a way to access parameters from the routing condition expressions?
回答1:
I opened a PR at https://github.com/symfony/symfony/pull/12869 for 2.7 version, until it's done and shipped you will have to use your own extended version if UrlMatcher in your project in which you'll need to add container functions provider and also inject a container in "variables" argument of ExpressionLanguage#evaluate so that you'll be able to access parameters and services.
You can look at PR to get a hint how to do it, I'll write about it more in detail if you need later.
回答2:
You are lucky (and I a bit late), I'm accessing to Symfony parameters from an expression just using the syntax %variable%.
Here are 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%
来源:https://stackoverflow.com/questions/26606836/access-global-parameters-from-route-condition-expressions-in-symfony