Redirecting in service - symfony2

前端 未结 5 473
南方客
南方客 2021-01-17 22:03

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

5条回答
  •  离开以前
    2021-01-17 22:50

    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;
    

提交回复
热议问题