May I perform redirection to another controller within service?
I have implemented a service based on example provided by @Artamiel.
My function code which i
This is possible but I would not recommend it. To redirect from service you should provide proper API
for you event for example like in: FOSUserBundle
where you are receiving instance of FilterUserResponseEvent
and you can set response on that event object:
class RegistrationListener implements EventSubscriberInterface
{
// dependencies
// ...
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onSuccess',
);
}
public function onSuccess(FilterUserResponseEvent $event)
{
$url = $this->router->generate('some_url');
$event->setResponse(new RedirectResponse($url));
}
}
This is possible thanks to controller:
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
if (null === $response = $event->getResponse()) {
// create response
}
// return response from event