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;
}
<?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
}
}
<?php
/*
* CMyController class file.
* Extends CController and we can modify it for certain purpose
*/
class CMyController extends CController
{
public $stash = array();
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{
$this->stash['member'] = Member::model()->find(Yii::app()->user->id);
}
}
return true;
}
}
SiteController:
<?php
class SiteController extends CMyController
{
public function actionSetting()
{
//setting:
$this->stash['xxx'] = array('xyz'=>'touya','xdfa'=>'hello');
$this->stash['yyy'] = array('bbb'=>'touya','cccc'=>'hello');
//setting:
$this->render('setting');
}
and then view:
// in view: site/setting.php
<?=var_dump($this->stash['xxx'])?><br/>
<?=var_dump($this->stash['yyy'])?><br/>
member last login time: <?= $this->stash['member']->last_login_time ?>