从Symfony中的路由条件表达式访问全局参数 [英] Access global parameters from route condition expressions in Symfony

查看:101
本文介绍了从Symfony中的路由条件表达式访问全局参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过路由条件(

I'm trying to access app-wide symfony parameters (defined in app/config/parameters.yml) from an expression in a route condition (documentation).

我很幸运地将参数插入百分号内并通过函数参数"(如DI

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:

语法错误-位置24附近不存在函数参数".

SyntaxError - The function "parameter" does not exist around position 24.


有没有办法从路由条件表达式中访问参数?


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

推荐答案

我在 https打开了PR 2.7版本的://github.com/symfony/symfony/pull/12869 ,直到完成并交付为止,如果您的项目中需要使用UrlMatcher,则您必须使用自己的扩展版本,而您需要在其中添加容器函数提供程序,并在ExpressionLanguage#evaluate的变量"参数中注入一个容器,以便您可以访问参数和服务.

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.

您可以查看PR,以获取有关操作方法的提示,如果以后需要,我会对其进行详细介绍.

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.

这篇关于从Symfony中的路由条件表达式访问全局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆