本文实例讲述了Yii控制器中filter过滤器用法。分享给大家供大家参考,具体如下:
指定过滤动作,(如下projectContext()方法在新建,列表,管理页面调用时使用)
1
2
3
4
5
6
7
8
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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程序设计有所帮助。