在模块Yii Framework中创建AccessRules

时间:2014-05-12 15:35:39

标签: yii

模块\ administrator \ components \ AdminController中的

class AdminController extends CController
{
    public function filters()
    {
        return array('accessControl');
    }

    public function accessRules()
    {
        return array(
            array('deny', 'users' => array('*')),
        );
    }
}
模块\ administrator \ controllers \ Sitecontroller中的

class SiteController extends AdminController
{
    public function actionIndex()
    {
        $this->render('index');
    }

        public function actionLogin()
        {
                $this->render('login');
        }
}

但是当访问mydomain.site/administrator/site/index时,它不会重定向到mydomain.site/administrator/site/login,它会重定向到mydomain.site/site/login(默认登录),我希望它重定向到mydomain.site/administrator/site/login

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

array('allow', // allow authenticated user to perform below actions
      'actions'=>array('index'),
      'users'=>array('@'),
), 
array('deny',  // deny all users
      'users'=>array('*'),
),

这样,当为site/index请求网址并且我们已将index设置为仅允许经过身份验证的用户时,该请求将被重定向到site/login(或默认登录)。

我希望它有所帮助。

答案 1 :(得分:0)

在调用过滤器之前设置CWebUser::$loginUrl

class AdminModule extends CWebModule
{
 public function init()
  {
   $this->setImport(array(
    'admin.models.*',
   //'application.models.*',
    'admin.components.*',
  ));
 }

 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
 if ( !Yii::app()->user->checkAccess('admin') ) {
 if(Yii::app()->user->isGuest){
  $url = Yii::app()->createUrl(Yii::app()->user->loginUrl);
 Yii::app()->user->returnUrl = Yii::app()->createUrl('/admin/');
 Yii::app()->request->redirect($url);
}
 else {
  throw new CHttpException(403,'Have no permission');
 }
 }
 return true;
 }
 else
 return false;
 }
}