Yii无法在管理模块中找到操作登录

时间:2012-06-21 14:45:28

标签: module yii

请参阅

的以下代码
<?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,但这也无效。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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);

       ...