yeah

搜索

计数器

62827

链接

控制器基类做访问过滤[总结]

Jan 03, 2010 08:35:17 PM | Comments(1) | Category:yii | Tags:

写基类的beforeAction函数:

 

protected function beforeAction($action)
    {
        $route=$this->id.'/'.$action->id;
        if(!in_array($route,array('site/login','site/error','site/logout')))
        {
            if(Yii::app()->user->isGuest)
                Yii::app()->user->loginRequired();
            else
                throw new CHttpException(403,'You are not authorized to perform this operation.');
        }
        return true;
    }
 

框架扩展:

 

// preloading 'log' component       
'preload'=>array('log','access'),
----------------------------------------------       
// application components       
'components'=>array(               
    'log'=>array(                       
        'class'=>'CLogRouter',                       
        'routes'=>array(                               
            array(                                       
                'class'=>'CFileLogRoute',                                       
                'levels'=>'error,warning',
            ),                       
        ),               
    ),               
    'access'=>array(                       
        'class'=>'application.components.EAccess',               
    ),
//other code
)

 

<?php
class EAccess extends CApplicationComponent {       
     private $_routes=array();       
/**         * Initializes this application component.         * This method is required by the IApplicationComponent interface.         */      
    public function init()        {  
         parent::init();               
        //开始请求时,代码               
        if(!Yii::app()->user->isGuest){                       
            Yii::app()->attachEventHandler('onbeginRequest',array($this,'myaccess'));
         }else{  
        }       
    }       
/**         * 代码         */  
    public function myaccess()        {  
              //hello world       
     }       
}