Home >Backend Development >PHP Tutorial >YII Framework filter usage analysis, yiifilter_PHP tutorial
This article describes the YII Framework filter usage with examples. Share it with everyone for your reference, the details are as follows:
First of all, read the official documentation, what is a filter, its function, filter rules, how to define a filter, etc.
Then give a summary of the filter.
http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller
A filter is a piece of code that can be configured to execute before or after a controller action. For example, access control filters will be executed to ensure that the user is authenticated before performing the requested action; performance filters can be used to measure the time it takes for the controller to execute.
An action can have multiple filters. Filters are executed in the order they appear in the filter list. Filters can prevent actions and other subsequent filters from executing
Filters can be defined as methods of a controller class. The method name must start with filter. For example, the existing filterAccessControl method defines a filter named accessControl. The filter method must be of the following structure:
public function filterAccessControl($filterChain) { // 调用 $filterChain->run() 以继续后续过滤器与动作的执行。 }
The $filterChain (filter chain) is an instance of CFilterChain, representing a list of filters related to the requested action. Within the filter method, we can call $filterChain->run() to continue executing subsequent filters and actions.
A filter can also be an instance of CFilter or one of its subclasses. The following code defines a new filter class:
class PerformanceFilter extends CFilter { protected function preFilter($filterChain) { // 动作被执行之前应用的逻辑 return true; // 如果动作不应被执行,此处返回 false } protected function postFilter($filterChain) { // 动作执行之后应用的逻辑 } }
To apply filters to an action, we need to override the CController::filters() method. This method should return an array of filter configurations. For example:
class PostController extends CController { ...... public function filters() { return array( 'postOnly + edit, create', array( 'application.filters.PerformanceFilter - edit, create', 'unit'=>'second', ), ); } }
The above code specifies two filters: postOnly and PerformanceFilter. The postOnly filter is method-based (the corresponding filter method has been defined in CController); while the performanceFilter filter is object-based. The path alias application.filters.PerformanceFilter specifies that the filter class file is protected/filters/PerformanceFilter. We configure the PerformanceFilter with an array so that it can be used to initialize the filter object's property values. Here the unit property value of PerformanceFilter will be initialized to second.
Using the plus and minus signs, we can specify which actions should or should not have the filter applied. In the above code, postOnly should be applied only to edit and create actions, while PerformanceFilter should be applied to actions other than edit and create. If the plus or minus sign is not used in the filter configuration, this filter will be applied to all actions.
Filter function:
Used to filter visitors and data and record access operations
How to use:
One as a method of controller. The method name starts with filter.
public function filterAccessControl($filterChain) { echo "--->filterAccessControl"; $filterChain->run(); }
Second, define the opposite filter class, which requires extends CFilter.
CFilter
<?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 <qiang.xue@gmail.com> * @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 <code>$filterChain->run()</code> * 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) { } }
The following examples illustrate the use of two filter rules:
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
You can see the specific execution process of filter above.
There are -,
in filters
The specific function is
Indicates that it only acts on these actions
- followed by a list of action names. Indicates exclusion.
If there is no -, all actions will be applied
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "php Date and Time" Usage Summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.