0

看下面的代码

<?php

class DefaultController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
        $model = new LoginForm;

        // collect user input data
        if (isset($_POST['LoginForm']))
        {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    public function filters()
    {
        return array(
            'accessControl',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform the 'login' action
                'actions' => array('login'),
                'users' => array('*'),
            ),
            array('allow', // allow the admin user to perform everything
                'actions' => array('*'),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

}

问题是,当我转到我的模块时:/?r=admin我被重定向到/?r=site/index我的默认站点控制器。

这怎么可能发生?

编辑:(添加 AdminModule)

<?php

class AdminModule extends CWebModule
{

    public function init()
    {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));

        $this->setComponents(array(
            'user' => array(
                'loginUrl' => Yii::app()->createUrl('admin/default/login'),
            )
        ));
    }

    public function beforeControllerAction($controller, $action)
    {
        if (parent::beforeControllerAction($controller, $action))
        {
            // this method is called before any module controller action is performed
            // you may place customized code here

            return true;
        }
        else
            return false;
    }

}

如您所见,我通过 setComponents 添加了 loginUrl,但这也不起作用。

4

2 回答 2

2

如果您没有登录用户,访问控制过滤器将启动并将您重定向到“家”。您可以在控制器上执行的唯一操作是“登录”,因此 index 是被拒绝操作的一部分。

于 2012-06-21T14:57:12.070 回答
1
class MyModule extends CWebModule
{

    public function init()
    {

        ...

        Yii::app()->setComponents(array(
            'user'=>array(
                'class'=>'CWebUser',
                'stateKeyPrefix'=>'My',
                'loginUrl'=>Yii::app()->createUrl($this->getId().'/default/login'),
            ),
        ), false);

       ...
于 2012-07-22T07:24:36.390 回答