在 Twig 模板中获取当前 URL? [英] Get current URL in Twig template?
本文介绍了在 Twig 模板中获取当前 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我四处寻找代码以获取 Twig 模板中的当前路径(而不是完整的 URL),即我不要http://www.sitename.com/page
,我只需要/page
.
I looked around for the code to get the current path in a Twig template (and not the full URL), i.e.
I don't want http://www.sitename.com/page
, I only need /page
.
推荐答案
{{ path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) }}
如果要将其读入视图变量:
If you want to read it into a view variable:
{% set currentPath = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) %}
app
全局视图变量包含各种有用的快捷方式,例如 app.session
和 app.security.token.user
,引用您可能在控制器中使用的服务.
The app
global view variable contains all sorts of useful shortcuts, such as app.session
and app.security.token.user
, that reference the services you might use in a controller.
这篇关于在 Twig 模板中获取当前 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文