加载中...

Yii-控制器- filter过滤器实现步骤


指定过滤动作,(如下projectContext()方法在新建,列表,管理页面调用时使用)
public function filters()
{
	return array(
		'accessControl', // perform access control for CRUD operations
		'postOnly + delete', // we only allow deletion via POST request
		'projectContext +create index admin',
	);	
}
添加一些过滤器逻辑,定义过滤方法,过滤方法前必加filter
public function filterProjectContext($filterChain)
{
	$prijectId = null;
	if(isset($_POST['pid'])){
		$projectId = $_POST['pid'];
	}elseif(isset($_GET['pid'])){
		$projectId = $_GET['pid'];
	}
	$this->loadProject($projectId);
	$filterChain->run();
}

public function loadProject($projectid)
{
	if($this->_project === null){
		$this->_project = Project::model()->findbyPK($projectid);
		if($this->_project === null){
			throw new CHttpException(404,'请求和项目没找到!');
		}
	}
	return $this->_project;
}



还没有评论.