I'm using Yii2 Advance application. I've SiteController.php
where I've actions like login
, logout
& index
. Here login
is for Guest users and index
& logout
for logged-in users. Now I've created one more action called reset
to provide Forgot Password functionality. But whenever I'm trying to call reset
action its redirecting back me to login page.
Following is my controller:
namespace backend\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;
/**
* Site controller
*/
class SiteController extends Controller {
/**
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['reset'],
'roles' => ['?'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post', 'get'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionIndex() {
return $this->render('index');
}
public function actionLogin() {
$this->layout = 'guest';
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
public function actionLogout() {
Yii::$app->user->logout();
return $this->goHome();
}
public function actionReset() {
return $this->render('reset');
}
}
I've added proper roles & behaviours
for it but still its not working. I tried adding few more actions as well but in-fact its not allowing me to render any other action except login.
Any help would be appreciated.
Solved it myself :)
There's condition in
vendor/yiisoft/yii2/web/Controller.php
i.e.if($this->action->id != 'login') { .. }
And I changed it to
if($this->action->id != 'login' && $this->action->id != 'reset') { .. }
i.e.
And its working.