yii2 redirect in init work or not work? -
class testcontroller extends controller { function init() { if(!(strtolower(yii::$app->requestedroute) == "account/login" || empty(yii::$app->requestedroute))) { if (false===$this->islogin()) { return $this->redirect('/login'); //it's work, redirect } } else { if($this->islogin()) { return $this->redirect('/purchaser/manifest'); //not work, won't redirect //echo $this->redirect('/purchaser/manifest'); //work } } } } i had override controller. when tried filter, found problem. i'm confused, help?
from yii2 docs:
in case action should not run, request should handled inside of
beforeactioncode either providing necessary output or redirecting request. otherwise response empty.
public function beforeaction($action) { if (!parent::beforeaction($action)) { return false; } // custom code here //eg if(something){ $this->redirect('/login'); return false; //not run action } return true; // continue run action } yii2 not care return value of init() function. applications still continue run action. redirect() function, not work until application completed (beforeaction return false or action return). when call redirect() in init() function nothing happens.
return $this->redirect('/login'); works. why? because in case, application redirect after run action.
can try, write $this->redirect('/login'); still works.
Comments
Post a Comment