Symfony 获取当前路由细枝
我想在twig上获取当前路径,我使用了这两个代码,但总是失败 代码1:Symfony 获取当前路由细枝,symfony,twig,Symfony,Twig,我想在twig上获取当前路径,我使用了这两个代码,但总是失败 代码1: {% if app.request.get('_route') == 'my_route' %} //Do something {% endif %} 代码2: {% if app.request.attributes.get == 'my_route' %} //Do something {% endif %} 使用URL末尾的“app_dev.php”调试并检查底部使用的路由。例如,我在
{% if app.request.get('_route') == 'my_route' %}
//Do something
{% endif %}
代码2:
{% if app.request.attributes.get == 'my_route' %}
//Do something
{% endif %}
使用URL末尾的“app_dev.php”调试并检查底部使用的路由。例如,我在这里显示“route1”:
然后在细枝模板中,您可以使用以下内容:
{% if app.request.attributes.get('_route') == 'route1' %}
<h1>test</h1>
{% endif %}
{%if-app.request.attributes.get('u-route')=='route1'}
测试
{%endif%}
我证实了这一点。不过我用的是Symfony3
也可以尝试使用以下方法代替“app.request.attributes.get()”:
- app.request.pathinfo
- app.request.uri
{% if app.request.attributes.get('_route') == '' %}
<h1>test</h1>
{% endif %}
{%if-app.request.attributes.get(''路由')=''%}
测试
{%endif%}
app.request.get(“U路线”)返回什么?这应该可以正常工作。@AlainTiemblo的可能副本我转储了它,它显示为“null”…你能告诉我们你是如何从你的控制器渲染你的文件的吗?路由在左下角的“@”符号处显示了什么?然后你试过{%if-app.request.attributes.get(“'u-route')='categories\u-index'}
看看这是否有效吗?