4

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

strangeQuirks
  • 4,761
  • 9
  • 40
  • 67
  • Possible duplicate of [get current url in twig template?](http://stackoverflow.com/questions/9378714/get-current-url-in-twig-template) – medunes Feb 19 '17 at 13:45
  • 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 Feb 19 '17 at 13:48
  • Actually would be this:$_SERVER["REQUEST_URI"]. Is there a helper service to get this data? A wrapper? – strangeQuirks Feb 19 '17 at 15:00

2 Answers2

7

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');
panche14
  • 651
  • 4
  • 8
0

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>
Community
  • 1
  • 1
medunes
  • 541
  • 3
  • 16
  • I did try that and it prints this: current route name : current url: http : //localhost:8000/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3DAppBundle%253AApplication%255CHeader%253Aheader – strangeQuirks Feb 19 '17 at 17:13