YII Blog
Yeah Cheung 's Yeah Cheung
yeah
如需合作,请发邮件! (realloveyou@foxmail.com)
热门文章
搜索
计数器
64545
最新留言
链接
最新评论
控制器基类做访问过滤[总结]
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;
}
{
$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
)
'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 {
/** * 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
}
}