yii过滤器的原理和使用
2013-12-24 11:02
435 查看
过滤器官方页面:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller
过滤器是一段代码,可被配置在控制器动作执行之前或之后执行。例如, 访问控制过滤器将被执行以确保在执行请求的动作之前用户已通过身份验证;性能过滤器可用于测量控制器执行所用的时间。
一个动作可以有多个过滤器。过滤器执行顺序为它们出现在过滤器列表中的顺序。过滤器可以阻止动作及后面其他过滤器的执行
过滤器可以定义为一个控制器类的方法。方法名必须以
其中的
要对动作应用过滤器,我们需要覆盖
上述代码指定了两个过滤器:
使用加减号,我们可指定哪些动作应该或不应该应用过滤器。上述代码中,
/////////////////////////////////////////////////////////////////////////
SiteController.php
下面举例说明两种filter规则的使用:
/www/yii_dev/testwebap/protected# tree
.
├── commands
│ ├── shell
│ ├── TestCommand.php
│ └── TestCommand.php~
├── components
│ ├── Controller.php
│ └── UserIdentity.php
├── config
│ ├── console.php
│ ├── main.php
│ └── test.php
├── controllers
│ ├── post
│ │ └── UpdateAction.php
│ ├── SiteController.php
│ ├── TestTestController.php
│ └── UserController.php
├── filters
│ └── MyFilter.php
MyFilter.php
http://www.localyii.com/testwebap/index.php?r=site/print
--->filterAccessControl--->print action
http://www.localyii.com/testwebap/index.php?r=site/create
-->MyFilter-->pre--->create action-->MyFilter-->post
-------------------------------------------------------
http://www.localyii.com/testwebap/index.php?r=site/print
--->filterAccessControl-->MyFilter-->pre--->print action-->MyFilter-->post
以上可以看到filter的具体执行流程。
在filters中有-、+
具体功能是
+表示仅仅作用于这一些action
-后边跟action名称列表。表示排除在外。
如果没有-、+则会应用的所有的action
过滤器是一段代码,可被配置在控制器动作执行之前或之后执行。例如, 访问控制过滤器将被执行以确保在执行请求的动作之前用户已通过身份验证;性能过滤器可用于测量控制器执行所用的时间。
一个动作可以有多个过滤器。过滤器执行顺序为它们出现在过滤器列表中的顺序。过滤器可以阻止动作及后面其他过滤器的执行
过滤器可以定义为一个控制器类的方法。方法名必须以
filter开头。例如,现有的
filterAccessControl方法定义了一个名为
accessControl的过滤器。 过滤器方法必须为如下结构:
public function filterAccessControl($filterChain) { // 调用 $filterChain->run() 以继续后续过滤器与动作的执行。 }
其中的
$filterChain(过滤器链)是一个 CFilterChain 的实例,代表与所请求动作相关的过滤器列表。在过滤器方法中, 我们可以调用
$filterChain->run()以继续执行后续过滤器和动作。过滤器也可以是一个 CFilter 或其子类的实例。如下代码定义了一个新的过滤器类:
class PerformanceFilter extends CFilter { protected function preFilter($filterChain) { // 动作被执行之前应用的逻辑 return true; // 如果动作不应被执行,此处返回 false } protected function postFilter($filterChain) { // 动作执行之后应用的逻辑 } }
要对动作应用过滤器,我们需要覆盖
CController::filters()方法。此方法应返回一个过滤器配置数组。例如:
class PostController extends CController { ...... public function filters() { return array( 'postOnly + edit, create', array( 'application.filters.PerformanceFilter - edit, create', 'unit'=>'second', ), ); } }
上述代码指定了两个过滤器:
postOnly和
PerformanceFilter。
postOnly过滤器是基于方法的(相应的过滤器方法已在 CController 中定义); 而
performanceFilter过滤器是基于对象的。路径别名
application.filters.PerformanceFilter指定过滤器类文件是
protected/filters/PerformanceFilter。我们使用一个数组配置
PerformanceFilter,这样它就可被用于初始化过滤器对象的属性值。此处
PerformanceFilter的
unit属性值将被初始为
second。
使用加减号,我们可指定哪些动作应该或不应该应用过滤器。上述代码中,
postOnly应只被应用于
edit和
create动作,而
PerformanceFilter应被应用于 除了
edit和
create之外的动作。 如果过滤器配置中没有使用加减号,则此过滤器将被应用于所有动作。
/////////////////////////////////////////////////////////////////////////
SiteController.php
下面举例说明两种filter规则的使用:
<?php class SiteController extends Controller { public function init() { //$this->layout='mylayout'; } public function filters() { return array( 'AccessControl - create', array( 'application.filters.MyFilter + create', ), ); } public function filterAccessControl($filterChain) { echo "--->filterAccessControl"; $filterChain->run(); } public function actionCreate() { echo "--->create action"; } public function actionPrint() { echo "--->print action"; }
/www/yii_dev/testwebap/protected# tree
.
├── commands
│ ├── shell
│ ├── TestCommand.php
│ └── TestCommand.php~
├── components
│ ├── Controller.php
│ └── UserIdentity.php
├── config
│ ├── console.php
│ ├── main.php
│ └── test.php
├── controllers
│ ├── post
│ │ └── UpdateAction.php
│ ├── SiteController.php
│ ├── TestTestController.php
│ └── UserController.php
├── filters
│ └── MyFilter.php
MyFilter.php
<?php class MyFilter extends CFilter { protected function preFilter ($filterChain) { // logic being applied before the action is executed echo "-->MyFilter-->pre"; return true; // false if the action should not be executed } protected function postFilter ($filterChain) { echo "-->MyFilter-->post"; } }
http://www.localyii.com/testwebap/index.php?r=site/print
--->filterAccessControl--->print action
http://www.localyii.com/testwebap/index.php?r=site/create
-->MyFilter-->pre--->create action-->MyFilter-->post
-------------------------------------------------------
public function filters() { return array( 'AccessControl - create', array( 'application.filters.MyFilter + create,print', ), ); }
http://www.localyii.com/testwebap/index.php?r=site/print
--->filterAccessControl-->MyFilter-->pre--->print action-->MyFilter-->post
以上可以看到filter的具体执行流程。
在filters中有-、+
具体功能是
+表示仅仅作用于这一些action
-后边跟action名称列表。表示排除在外。
如果没有-、+则会应用的所有的action
相关文章推荐
- Yii乐观锁的使用及原理
- Yii中小物件save()方法原理与使用
- 浅谈Yii乐观锁的使用及原理
- 【过滤器】(1)过滤器原理、使用方式的简单介绍
- Yii 1.1 过滤器原理学习
- YII Framework学习教程-YII的V-view的基本使用和实现原理-2011-11-16
- 过滤器原理及使用
- PHP的Yii框架中过滤器相关的使用总结
- 学习 yii 框架 – 使用简单的动作过滤器
- yii Controller 访问控制过滤器 使用 摘记
- PHP的Yii框架中过滤器相关的使用总结
- 关于函数strtok和strtok_r的使用要点和实现原理(一)
- Azure Stack深入浅出5:在Azure Stack上使用Web App PaaS服务及其背后原理窥探
- vector原理及其成员函数使用总结
- Java的MyBatis框架中Mapper映射配置的使用及原理解析
- yii2下使用自带类发送邮件
- eQEP增强型正交编码模块原理介绍及配合MircoE mercury II 4000光栅传感器使用
- Android系统开发中log的使用方法及简单的原理
- linux下防火墙iptables原理及使用
- 弱网测试原理与方法(二)-使用虚拟机模拟网络速率