Контроллер Yii2 не разрешает другое действие гостевому пользователю

Я использую приложение Yii2 Advance. Я SiteController.php где у меня такие действия, как login, logout & index, Вот login для гостей и index & logout для зарегистрированных пользователей. Теперь я создал еще одно действие под названием reset обеспечить функциональность забытого пароля. Но всякий раз, когда я пытаюсь позвонить reset действие его перенаправить обратно на страницу входа.

Следующий мой контроллер:

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');
}

}

Я добавил правильный roles & behaviours для этого, но все еще его не работает. Я также попытался добавить еще несколько действий, но на самом деле это не позволило мне выполнить любое другое действие, кроме входа в систему.

Любая помощь будет оценена.

0

Решение

Решил это сам 🙂

Там в состоянии vendor/yiisoft/yii2/web/Controller.php то есть

if($this->action->id != 'login') { .. }

И я изменил это на

if($this->action->id != 'login' && $this->action->id != 'reset') { .. }

то есть

public function beforeAction($action)
{
// If user is guest then redirect to login page
if($this->action->id != 'login' && $this->action->id != 'reset') {
if(!isset($_SESSION['__id'])) {
$this->redirect(array('site/login'));
}
}
if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
}
return true;
} else {
return false;
}
}

И это работает.

1

Другие решения

Попробуй это:

<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'reset'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],

],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}

/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionReset(){
//die('You are here');
return $this->render('reset');
}

...
}

Код моего сброса:

<?php

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

use yii\helpers\Html;

$this->title = "Reset";
?>
<div class="site-index">

<h1><?= Html::encode($this->title) ?></h1>

<div class="alert alert-success">
Hi here you are without login
</div></div>

введите описание изображения здесь

1

По вопросам рекламы [email protected]