4
votes

In my base.html.twig I render a component:

{% block header %}
    {{ render(controller("AppBundle:Application\\Header:header")) }}
{% endblock %}

Is there a way to get the current route action/controller? i.e. the current url in the browser?

When I do var_dump($request->get('_route'));die; it results in null

2
Possible duplicate of get current url in twig template? - medunes
Dont think so as when i try that, i get this: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "" as such route does not exist."). - strangeQuirks
Actually would be this:$_SERVER["REQUEST_URI"]. Is there a helper service to get this data? A wrapper? - strangeQuirks

2 Answers

7
votes

If you want get the actual route, in your controller you can get the master request like this:

$this->container->get('request_stack')->getMasterRequest()->get('_route');
0
votes

As I couldn't answer through a comment, I have to write this as an answer.

As mentionned above, this question might be a duplicate of: Get current URL in Twig template

To get both the name of the current route and the current URL you can simply render them within your twig template by:

<p><strong>current route name</strong> :{{app.request.attributes.get('_route')}}</p>
<p><strong>current url: </strong> {{ app.request.schemeAndHttpHost ~ app.request.requestUri }}</p>