php - Yii2: Execute a redirect from static non-controller context -


i have function calls our api called executegetrequest (with variants other http methods). static , located in base class. called controllers. if api returns 401 http status code, executegetrequest should redirect user logout page on "frontend" yii2 project in order clear out of date session data.

my coworker , have tried multiple different ways redirect. $this->redirect() not work because there no $this object static context.

return yii::$app->getresponse()->redirect(url::to('login/user-logout')); 

does not work.

yii::$app->getresponse()->redirect(url::to('login/user-logout'))->send(); return; 

does not work. tried these , without url::to().

i able yii::trace() in conditional checked 401 response. works fine. problem not detection of 401 status codes, redirect.

this should work

yii::$app->response->redirect(['login/user-logout'])->send(); return; 

possible causes why it's not working in case:

  1. this ajax/pjax request.
  2. you have used redundant url::to() wrong route.
  3. you expecting post request case logout actions.

401 means it's #3. try same redirection mechanism pointing other route and/or remove verb behavior user-logout post action verify it.


Comments