yeah

搜索

计数器

64550

链接

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

yeah posted @ Jan 03, 2010 08:35:17 PM in yii , 989 阅读

写基类的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       
     }       
}
celeb networth 说:
Apr 26, 2022 06:42:35 PM

If you want to find the net worth and some information about a celebrity, here's the website celebrity networth for you.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter