yii2 - Redirecting unauthorized users to another controller action -
i have controller code login:
public function actionlogin() { if (!\yii::$app->user->isguest) { return $this->redirect(yii::$app->request->baseurl.'/telephone/index'); } $model = new loginform(); if ($model->load(yii::$app->request->post()) && $model->login()) { return $this->redirect(yii::$app->request->baseurl.'/telephone/index'); } return $this->render('login', [ 'model' => $model, ]); }
and preventing add
, delete
action unauthorized users used:
public function behaviors() { return [ 'access' => [ 'class' => accesscontrol::classname(), 'only' => ['add','delete'], 'rules' => [ // allow authenticated users [ 'allow' => true, 'roles' => ['@'], ], // else denied default ], ], ]; }
but if unauthorized users clik add or delete, redirected site/login
. how can change controller , action?
there different approaches changing route depending on scope. involve changing loginurl
property of yii\web\user
class.
global
edit config file.
'components' => [ 'user' => [ 'loginurl' => ["controller/action"], ], ],
controller/action
edit beforeaction method of controller.
public function beforeaction($action) { // action-specific if(in_array($action->id,['not', 'allowed', 'actions'])) yii::$app->user->loginurl = ["controller/action"]; // controller-wide yii::$app->user->loginurl = ["controller/action"]; if (!parent::beforeaction($action)) { return false; } return true; }
Comments
Post a Comment