Get current URL in Twig template?

SymfonyTwig

Symfony Problem Overview


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.

Symfony Solutions


Solution 1 - Symfony

{{ 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.

Solution 2 - Symfony

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)

Solution 3 - Symfony

In symfony 2.1 you can use this:

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

In symfony 2.0, one solution is to write a twig extension for this

public function getFunctions()
{
    return array(
        'my_router_params' => new \Twig_Function_Method($this, 'routerParams'),
    );
}

/**
 * Emulating the symfony 2.1.x $request->attributes->get('_route_params') feature.
 * Code based on PagerfantaBundle's twig extension.
 */
public function routerParams()
{
    $router = $this->container->get('router');
    $request = $this->container->get('request');

    $routeName = $request->attributes->get('_route');
    $routeParams = $request->query->all();
    foreach ($router->getRouteCollection()->get($routeName)->compile()->getVariables() as $variable) {
        $routeParams[$variable] = $request->attributes->get($variable);
    }

    return $routeParams;
}

And use like this

{{ path(app.request.attributes.get('_route'), my_router_params()|merge({'additional': 'value'}) }}

You won't need all this unless you want to add additional parameters to your links, like in a pager, or you want to change one of the parameters.

Solution 4 - Symfony

You can get the current URL in Twig like this:

{{ app.request.schemeAndHttpHost ~ app.request.requestUri }}

Solution 5 - Symfony

It should be noted that if you have additional query parameters in your URL, which are not part of the configured route, the accepted answer will not include them in the current URL (path).

Why would you want extra parameters?

For example, if you have a list page with records that can be filtered by keyword and the page has pagination, most likely the query variables for "keyword" and "page" will not be in your route. But in your forward and back buttons for paging, you need the full current URL (that contains the keywords so the next page is still filtered). And you need to modify the page variable.

How to Merge In Extra Query Parameters

So you can get the current route, and merge in the extra variables (after modifying one or more of those extra variables). Note that you are merging in your own variables to the app.request.query.all, and then merging that array into the app.request.attributes.get('_route_params'). The path() method requires that you provide all the required parameters of the route, which is why you need to include the _route_params.

{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge(app.request.query.all|merge({'page': 2 }))) }}

That's really ugly, but if you are developing pagination, you will need to modify the page variable on each separate link, so you have to include the whole thing each time. Perhaps others have a better solution.

Solution 6 - Symfony

If you are using Silex 2 you can not access the Request object anymore.

You can access the current request attributes this way.

app.request_stack.currentrequest.attributes.get('_route')

And to generate the full current URL : path(app.request_stack.currentrequest.attributes.get('_route'), app.request_stack.currentrequest.attributes.get('_route_params'))

Solution 7 - Symfony

Using Symfony 5 you can use this:

{{ app.request.uri }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark CiborView Question on Stackoverflow
Solution 1 - SymfonyRodney FolzView Answer on Stackoverflow
Solution 2 - Symfonye382df99a7950919789725ceeec126View Answer on Stackoverflow
Solution 3 - SymfonyBártfai TamásView Answer on Stackoverflow
Solution 4 - SymfonyZaheer BabarView Answer on Stackoverflow
Solution 5 - SymfonyChadwick MeyerView Answer on Stackoverflow
Solution 6 - SymfonyTimView Answer on Stackoverflow
Solution 7 - SymfonyL3xpertView Answer on Stackoverflow