B
B
BonBon Slick2018-07-26 23:18:33
symfony
BonBon Slick, 2018-07-26 23:18:33

RedirectResponse in EventSubscriber?

An event occurs, I do checks, if there is no access or the condition is not met, I try to make a redirect

public static function getSubscribedEvents() : array
    {
        return [
            KernelEvents::EXCEPTION => [
                ['customMethodName', 0],
            ],
        ];
    }
....
public fucntion customMethodName(...)
{
         return  new RedirectResponse('/');

However, the current link is being updated, no redirect.
Runs this code
header('Location: https://site.loc/);
        exit;

Why do you say so please?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ig0r88, 2018-07-27
@BonBonSlick

Try like this:

public function customMethodName(GetResponseForExceptionEvent $event)
{
    $event->setResponse(new RedirectResponse(...));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question