Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig - Get URL for canonical tag

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?

like image 486
user1961082 Avatar asked Nov 03 '13 22:11

user1961082


2 Answers

This will work,

{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
like image 64
jamek Avatar answered Oct 20 '22 19:10

jamek


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 :)

like image 36
Jovan Perovic Avatar answered Oct 20 '22 19:10

Jovan Perovic