1

I need to redirect to other controller under certain circumstances, so I create the following onKernelController method:

public function onKernelController(FilterControllerEvent $event) {
    $controller = $event->getController();


    if (!is_array($controller)) {

        return;
    }

    $request = $event->getRequest();
    $route  = $request->attributes->get('_route');
    if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {

        return;
    }
    if ($controller[0])

    if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') && ($route != "show_pay_pending_invoices") ) {

        $client = $this->container->get('security.context')->getToken()->getUser();
        if ($client->getHasPendingInvoices()) {

            $request = new \Symfony\Component\HttpFoundation\Request();
            $request->attributes->set('_controller', 'FusionWebBundle:User:showPayPendingInvoices');
            $event->setController($this->resolver->getController($request)); 
        }
    }
}

It redirects well, but the problem is that it is algo redirecting controllers like: Symfony\Bundle\AsseticBundle\Controller\AsseticController FOS\JsRoutingBundle\Controller\Controller

so the page is show unstyled and with multiple errors. How can avoid redirecting that controllers?

Thanks

1
  • what onKernelController is? An event Listener/Subscriber? Because from your description it seems to be ... Commented Mar 22, 2014 at 12:47

2 Answers 2

1

check what information you have in FilterControllerEvent in your method, maybe you will find identifier which you can validate on the beginning of you method.

Otherwise, you need to create your own event and dispach it from that one particular controller you need, because it will be one and only place where event is dispached. Check documentation

Sign up to request clarification or add additional context in comments.

Comments

1

I know it's been some time since the question was posted, but I had similar problem, but could not find any identifier in FilterControllerEvent method to that I could use to validate. So I came up with simple solution. I've added:

private $executed;

and in __construct set it to false.

$this->executed = false;

Then at the end of onKernelController method I set it to true.

$this->executed = true;

This way I can check whether method was called.

if (!$this->executed) {
    // your code
}

Whole code example:

public function __construct(ContainerInterface $container)
{
    $this->executed = false;
}

public function onKernelController(FilterControllerEvent $event)
{
    // ....
    if (!$this->executed) {
        // your code
    }

    $this->executed = true;
}

Maybe not the best/cleanest solution, but works and does the job for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.