本文实例讲述了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;
}
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。