Yii2控制器不允许其他操作给访客用户

时间:2016-05-10 10:45:03

标签: php yii2 yii2-advanced-app yii2-rbac

我正在使用Yii2 Advance应用程序。我let overlay: UIView = UIView(frame: CGRectMake(0, 0, cell.imageView.frame.size.width, cell.imageView.frame.size.height)) overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1) cell.imageView.addSubview(overlay) 我在SiteController.phplogin和&amp ;; 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'); } } ,但仍然无效。我尝试添加更多动作,但事实上它不允许我渲染除登录之外的任何其他动作。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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>

enter image description here

答案 1 :(得分:1)

自己解决了:)

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

它的工作。