Symfony 2 - 如何使用bundle JMSI18nRoutingBundle获取当前路由名称

时间:2014-10-08 14:03:19

标签: php symfony internationalization bundle

我在Symfony 2项目中使用JMSI18nRoutingBundle来翻译我的路线。

我遇到了如何获取当前路由名称的问题(在Twig&#39s模板和Symfony控制器中)...

我试过了:

在Twig的模板中:

{{ app.request.attributes.get('_route') }}
{{ app.request.get('_route') }}

并在控制器中:

$this->container->get('request')->get('_route')
$this->container->get('request')->attributes->get('_route')

但没有成功......我认为是因为我使用JMSI18nRoutingBundle ......

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

没关系,我找到了解决方案:

$actual_link = preg_replace('/\/app_dev.php/', '', $_SERVER['REQUEST_URI']);
$route = $this->get('router')->match($actual_link)['_route'];

第一行:如果我们处于开发模式,则必须删除“app_dev.php”。
第二行:将当前网址与路由匹配,以查找当前路由名称。

相关问题