Yii Framework 过滤器 Filter 用法详解
本文详细解析 Yii Framework 中过滤器的使用机制,旨在为开发者提供实用的参考指南。
首先查阅官方文档,了解过滤器的基本概念、功能作用、规则设定及定义方式。
随后对过滤器的核心特性进行系统性总结。
参考官方文档地址:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller
过滤器是一段可配置的代码,能够在控制器动作执行的前后阶段介入。例如,访问控制过滤器用于确保用户在请求动作前已完成身份验证;性能过滤器则可用于记录控制器执行所消耗的时间。
单个动作可以关联多个过滤器,它们按照在列表中的出现顺序依次执行。过滤器具备阻断能力,一旦触发阻止条件,该动作及后续过滤器将不再执行。
过滤器可定义为控制器类的方法,方法名需以 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,可初始化其属性值,此处 unit 属性将被初始化为 second。
使用加减号前缀可指定过滤器适用的动作范围。在上述代码中,postOnly 仅应用于 edit 和 create 动作,而 PerformanceFilter 应用于除 edit 和 create 之外的所有动作。若未使用加减号,则该过滤器将作用于所有动作。
过滤器的主要功能包括:
对访问者和数据进行过滤,并记录访问操作日志。
具体使用方法分为两种:
一、作为 Controller 的一个方法,方法名必须以 filter 开头。
public function filterAccessControl($filterChain)
{
echo "--->filterAccessControl";
$filterChain->run();
}
二、定义独立的 Filter 类,该类必须继承自 CFilter。
CFilter 基类提供了 preFilter 和 postFilter 等保护方法供重写。
<?php
/**
* CFilter is the base class for all filters.
*
* A filter can be applied before and after an action is executed.
* It can modify the context that the action is to run or decorate the result that the
* action generates.
*
* Override {@link preFilter()} to specify the filtering logic that should be applied
* before the action, and {@link postFilter()} for filtering logic after the action.
*
* @author Qiang Xue
* @version $Id: CFilter.php 2799 2011-01-01 19:31:13Z qiang.xue $
* @package system.web.filters
* @since 1.0
*/
class CFilter extends CComponent implements IFilter
{
/**
* Performs the filtering.
* The default implementation is to invoke {@link preFilter}
* and {@link postFilter} which are meant to be overridden
* child classes. If a child class needs to override this method,
* make sure it calls $filterChain->run()
* if the action should be executed.
* @param CFilterChain $filterChain the filter chain that the filter is on.
*/
public function filter($filterChain)
{
if($this->preFilter($filterChain))
{
$filterChain->run();
$this->postFilter($filterChain);
}
}
/**
* Initializes the filter.
* This method is invoked after the filter properties are initialized
* and before {@link preFilter} is called.
* You may override this method to include some initialization logic.
* @since 1.1.4
*/
public function init()
{
}
/**
* Performs the pre-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on.
* @return boolean whether the filtering process should continue and the action
* should be executed.
*/
protected function preFilter($filterChain)
{
return true;
}
/**
* Performs the post-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on.
*/
protected function postFilter($filterChain)
{
}
}
下面通过实例说明两种 Filter 规则的具体应用:
SiteController.php
<?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 框架的 PHP 程序设计有所帮助。