Redirect response from Event Subscriber in Symfony PHP
up vote
0
down vote
favorite
I'm trying to return a permanent (301) redirect response from a event subscriber hooked into the kernel events in Synfony PHP.
My subscriber is as follow:
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentHttpFoundationRedirectResponse;
class KernelSubscriber implements EventSubscriberInterface
public function __construct()
// some stuff here but not relevant for this example
public static function getSubscribedEvents(): array
return [ KernelEvents::REQUEST => 'onRequest' ];
public function onRequest(GetResponseEvent $event): void
// if conditions met
// 301 redirect to some internal or external URL
if(!$event->isMasterRequest()) return;
If this were a controller I would return $this->redirectToRoute('route')
or something like that but returning from the onRequest
method is in a much different context.
How can I return a response (a redirect, in particular) from this event subscriber?
php symfony symfony4 php-7.2
add a comment |
up vote
0
down vote
favorite
I'm trying to return a permanent (301) redirect response from a event subscriber hooked into the kernel events in Synfony PHP.
My subscriber is as follow:
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentHttpFoundationRedirectResponse;
class KernelSubscriber implements EventSubscriberInterface
public function __construct()
// some stuff here but not relevant for this example
public static function getSubscribedEvents(): array
return [ KernelEvents::REQUEST => 'onRequest' ];
public function onRequest(GetResponseEvent $event): void
// if conditions met
// 301 redirect to some internal or external URL
if(!$event->isMasterRequest()) return;
If this were a controller I would return $this->redirectToRoute('route')
or something like that but returning from the onRequest
method is in a much different context.
How can I return a response (a redirect, in particular) from this event subscriber?
php symfony symfony4 php-7.2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to return a permanent (301) redirect response from a event subscriber hooked into the kernel events in Synfony PHP.
My subscriber is as follow:
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentHttpFoundationRedirectResponse;
class KernelSubscriber implements EventSubscriberInterface
public function __construct()
// some stuff here but not relevant for this example
public static function getSubscribedEvents(): array
return [ KernelEvents::REQUEST => 'onRequest' ];
public function onRequest(GetResponseEvent $event): void
// if conditions met
// 301 redirect to some internal or external URL
if(!$event->isMasterRequest()) return;
If this were a controller I would return $this->redirectToRoute('route')
or something like that but returning from the onRequest
method is in a much different context.
How can I return a response (a redirect, in particular) from this event subscriber?
php symfony symfony4 php-7.2
I'm trying to return a permanent (301) redirect response from a event subscriber hooked into the kernel events in Synfony PHP.
My subscriber is as follow:
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentHttpFoundationRedirectResponse;
class KernelSubscriber implements EventSubscriberInterface
public function __construct()
// some stuff here but not relevant for this example
public static function getSubscribedEvents(): array
return [ KernelEvents::REQUEST => 'onRequest' ];
public function onRequest(GetResponseEvent $event): void
// if conditions met
// 301 redirect to some internal or external URL
if(!$event->isMasterRequest()) return;
If this were a controller I would return $this->redirectToRoute('route')
or something like that but returning from the onRequest
method is in a much different context.
How can I return a response (a redirect, in particular) from this event subscriber?
php symfony symfony4 php-7.2
php symfony symfony4 php-7.2
asked Nov 11 at 0:24
yevg
392622
392622
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Should be something like:
$event->setResponse(new RedirectResponse($route));
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Should be something like:
$event->setResponse(new RedirectResponse($route));
add a comment |
up vote
2
down vote
accepted
Should be something like:
$event->setResponse(new RedirectResponse($route));
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Should be something like:
$event->setResponse(new RedirectResponse($route));
Should be something like:
$event->setResponse(new RedirectResponse($route));
answered Nov 11 at 0:31
Alex.Barylski
80221942
80221942
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244734%2fredirect-response-from-event-subscriber-in-symfony-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown