1

I need to get a route of the main action in the another one, that has been rendered in a template. How to get the value?

If I try to get the route from $this->container->get('request')->get('_internal') or $this->container->get('request')->get('_internal') they are NULLs.

Currently, I have only some workaround like this

{% render(controller("DemBackendBundle:Default:changeWebsite", {
    'redirect': app.request.attributes.get('_route') 
})) %}

but I read here https://github.com/symfony/symfony/issues/854 that is not good way to go.

deem
  • 1,844
  • 4
  • 20
  • 36

1 Answers1

0

What you seek will be "impossible" until Symfony 2.4 comes out (it's being developed at the moment and it has no stable releases yet).

In Symfony2.4 you will be able to use RequestStack.

However, I've put quotes around impossible for a reason, because it is doable, but it isn't pretty:

  • Create a service
  • Make it listen to kernel.request event
  • In method which you defined to be called when event occurs, you will get GetResponseEvent as parameter.
  • Get reference to request out of it using GetResponseEvent::getRequest method.
  • Check if request is master request like this:

        if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
            // extract route
        }
    
  • If it is, extract route

So basically your service should look something like this:

<?php

namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;

class AcmeRequestListener
{
    /**
     * @var string
     */
    private $route;

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
            $this->route = $event->getRequest()->get('_route');
        }
    }
}

Now, since I see you need route name in your template, you can make it extend twig extension, and of course tag it as twig extension:

<?php

namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;

class AcmeRequestListener extends \Twig_Extension
{
    /**
     * @var string
     */
    private $route;

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
            $this->route = $event->getRequest()->get('_route');
        }
    }

    public function getFunctions()
    {
         return [
             new \Twig_SimpleFunction('current_route', function () {
                 return $this->route;
             }),
         ];
    }
}

This code is out of head and I haven't tested it, so sorry if i misspelled some class or function name :)

Now in your templates you should be able to do:

{% block something %}
    Hi, route of my master request is: {{ current_route() }}
{% endblock %}
Igor Pantović
  • 9,107
  • 2
  • 30
  • 43
  • uhhh... it sounds pretty difficult for this basic purpose. – deem Dec 03 '13 at 22:10
  • Well, there is 8 effective lines of code total and 5 yaml lines needed in config to tag and configure this class as service making it 13 lines :) . As I said, it isn't pretty but does the job and after you do it, you are left with pretty elegant way of getting route in templates – Igor Pantović Dec 03 '13 at 22:18
  • 1
    Guess you're in luck: Symfony 2.4 stable has been released today: http://symfony.com/blog/symfony-2-4-0-released :) – Igor Pantović Dec 03 '13 at 22:25