Service redirection - symfony2

Is it possible to redirect to another controller in the service?

I implemented a service based on the @Artamiel example.

My function code that is executed by the controller is as follows:

public function verifyanddispatch() { $session = $this->request->getSession(); if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->router->generate('app_listbooks')); } 

I checked, and !$session->get("App_Books_Chosen_Lp") is true. However, I am not redirected to the app_listbooks controller.

I think this is because I am returning the redirect response not directly to the controller, and not to the service.

+2
source share
5 answers

I don’t know how you defined your service, but the example below works fine. Assuming you are calling a service on your controller.

services.yml

 services: application_backend.service.user: class: Application\BackendBundle\Service\UserService arguments: - @router 

Class of service

 namespace Application\BackendBundle\Service; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; class UserService { private $router; public function __construct( RouterInterface $router ) { $this->router = $router; } public function create() { $user = new User(); $user->setUsername('hello'); $this->entityManager->persist($user); $this->entityManager->flush(); return new RedirectResponse($this->router->generate('application_frontend_default_index')); } } 

controller

 public function createAction(Request $request) { //......... return $this->userService->create(); } 

UPDATE


Although the original answer above answers the original question, this is not the best practice, so do the following if you want better. First of all, do not put @router into operation and make the following changes.

 // Service public function create() { ..... $user = new User(); $user->setUsername('hello'); $this->entityManager->persist($user); $this->entityManager->flush(); ..... } // Controller public function createAction(Request $request) { try { $this->userService->create(); return new RedirectResponse($this->router->generate(........); } catch (......) { throw new BadRequestHttpException(.....); } } 
+8
source

From an architectural point of view, you should not create redirectResponse in a service. One way could be to throw an exception into a service that is caught by controllers, where you can easily create a RedirectResponse.

An example can be found here: Redirect from service to Symfony2

+3
source

It seems that you should pass the redirection from the service to the controller as an object or RedirectResponse value, which will tell the controller that everything inside the service went fine and redirection is not required (in the example below: "true" value). Then you must check in the controller what value was provided (whether it is "true" or RedirectResponse ), and either return RedirectResponse again to the controller or do nothing.

The example below should tell you everything.

Services:

 <?php namespace AppBundle\Service; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\HttpFoundation\RedirectResponse; class nobookchoosenService { /* @var $request Request */ private $request; /* @var $router RouterInterface */ private $router; public function __construct(RequestStack $requestStack, RouterInterface $router) { $this->request = $requestStack->getCurrentRequest(); $this->router = $router; } public function verifyanddispatch() { $session = $this->request->getSession(); if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->router->generate('app_listbooks')); else return true; } } 

Controller:

 $checker = $this->get('nobookchoosen_service')->verifyanddispatch(); if($checker != "true") return $checker; 
0
source

I agree with @Moritz that redirects should be done inside controllers.

But sometimes it happens that some redirection would be useful if it was performed from a local function inside the controller.

In this case, the solution could be to create a custom exception class (RedirectException).

Here you can go to an interesting post: https://www.trisoft.ro/blog/56-symfony-redirecting-from-outside-the-controller .

I know this is debatable, but it makes sense if it is used in a local function inside its controller.

To summarize, follow these steps:

1) Create a RedirectException class

2) Create a kernel listener

3) Configure the service in the configuration service file (service.yml)

4) Throw an exception with a URL to redirect if necessary.

0
source

It is possible, but I would not recommend it. To redirect from a service, you must specify an API for your event, for example, for example: FOSUserBundle , where you get an instance of FilterUserResponseEvent , and you can set the response to this 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 the controller:

 $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event); if (null === $response = $event->getResponse()) { // create response } // return response from event 
0
source

All Articles