aqiang 2011-08-24 11:27:14 4086次浏览 5条回复 0 0 0

这个项目是修改以前遗留的问题, 有个模块不需要用户登录就可以访问,控制器里做了这样的设置

public function filters() {
  return array(
    'accessControl -playlist', // perform access control for CRUD operations
  );
}

但是view里面还有诸如 Yii::app()->user()->uid 这样的代码 ,如果不登录就报错了。。。

我想是每个地方 去判断 Yii::app()->user()->isGuest 的值,这样做是不是太麻烦了,有简单的方法吗?

  • 回复于 2011-08-24 11:55 举报

    不需要。。。。。。。

  • 回复于 2011-08-24 11:38 举报

    是否意味着,view也要重新弄一遍?

  • 回复于 2011-08-24 11:34 举报

    这么说吧,现在的控制器不都是继承的ccontroller吗?这个控制器默认是不需要登陆就可以访问的。
    你可以自己写一个Controller。然后在这个控制里面添加必须登陆才可以访问。需要登陆的模块就继承Controller.不要登陆的还是继承ccontroller。

    <?php
    /**
     * Controller is the customized base controller class.
     * All controller classes for this application should extend from this base class.
     */
    class Controller extends CController
    {
      /**
      * @var string the default layout for the controller view. Defaults to  'application.views.layouts.column1',
      * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
      */
      public $layout='application.views.layouts.main';
      /**
      * @var array context menu items. This property will be assigned to {@link CMenu::items}.
      */
      public $menu=array();
      /**
      * @var array the breadcrumbs of the current page. The value of this property will
      * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
      * for more details on how to specify this property.
      */
      public $breadcrumbs=array();
      public function beforeAction(){
        $c =  $this->id;
        $a =  $this->action->id;
    	
      if (!user()->isGuest  || $a=='login' || $a=='captcha'){
        return true;	
      }else{
        $this->redirect(url('/site/login'));
      }}
    }
    
  • 回复于 2011-08-24 11:31 举报

    大神,能说的再详细些吗?

  • 回复于 2011-08-24 11:29 举报

    可以自定义一个控制器。然后继承自定义的控制器

您需要登录后才可以回复。登录 | 立即注册