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
It seems to make the redirection from service you have to pass to controller either RedirectResponse
object or value that will tell controller that everything within service went fine and no redirection is needed (in example below: "true" value). Then you have to verify in controller which value was provided (whether it is "true" or RedirectResponse
) and either return RedirectResponse
again within controller or do nothing.
Example below should tell everything.
Service:
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;