php教程

超轻量级php框架startmvc

Yii控制器中filter过滤器用法分析

更新时间:2020-03-10 01:07:59 作者:startmvc
本文实例讲述了Yii控制器中filter过滤器用法。分享给大家供大家参考,具体如下:指定过滤

本文实例讲述了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 控制器 filter 过滤器