Get current URL in Twig template?
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')) %}
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.
Get current url: {{ app.request.uri }}
in Symfony 2.3, 3, 4, 5
Get path only: {{ app.request.pathinfo }}
(without parameters)
Get request uri: {{ app.request.requesturi }}
(with parameters)