Twig - Get URL for canonical tag

扶醉桌前 提交于 2019-12-10 12:36:37

问题


I'm looking to create a dynamic rel="canonical" tag in my application which pulls in the current URL but want to ensure any query parameters are removed. E.g http://www.example.com/test/?page=2 should have the canonical as http://www.example.com/test/, therefore {{ app.request.uri }} doesn't work as this pulls in ?page=2 as well.

Does anyone know how to pull in the absolute path of a page without the query parameters?


回答1:


This will work,

{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}



回答2:


I just tried to dump baseUrl and can confirm that it does not work.

However, this works:

{{ app.request.getSchemeAndHttpHost ~ app.request.baseUrl ~ app.request.pathInfo }}

I know, it's not pretty but it does the job :)



来源:https://stackoverflow.com/questions/19758747/twig-get-url-for-canonical-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!