This example describes the filter filter usage for the YII framework. Share to everyone for your reference, specific as follows:
First look at the official description of the document, what is the filter, the role of filters, filter rules, the definition of filters and so on.
Then make 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 the controller action is executed. For example, an access control filter is executed to ensure that the user is authenticated before the requested action is performed, and the performance filter can be used to measure the time spent by the controller's execution.
An action can have more than one filter. Filter execution order is the order in which they appear in the filter list. Filters can block actions and other filters that follow
A filter can be defined as a method of a controller class. The method name must begin with filter. For example, the existing Filteraccesscontrol method defines a filter named AccessControl. The filter method must be of the following structure:
The Public Function Filteraccesscontrol ($filterChain)
{
//calls $filterChain->run () to continue the execution of subsequent filters and actions.
}
The $filterChain (filter chain) is an Cfilterchain instance that represents a list of filters associated with the requested action. In the filter method, we can call $filterChain->run () to continue to perform subsequent filters and actions.
A filter can also be an instance of a cfilter or its subclasses. The following code defines a new filter class:
Class Performancefilter extends Cfilter
{
protected function prefilter ($filterChain)
{
// The logical return true applied before the action is executed
;//If the action should not be executed, this returns false
}
protected function Postfilter ($filterChain)
{
//action applied after the execution of the logic
}
}
To apply the filter to the action, we need to overwrite the Ccontroller::filters () method. This method should return a filter configuration array. 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 based on the method (the corresponding filter method is already defined in Ccontroller), and the Performancefilter filter is object-based. The path alias Application.filters.PerformanceFilter specifies that the filter class file is Protected/filters/performancefilter. We use an array to configure Performancefilter so that it can be used to initialize the property values of the Filter object. The Unit property value for Performancefilter here will be initially second.
With the plus minus sign, we can specify which actions should or should not be applied to the filter. In the above code, POSTONLY should only be applied to edit and create actions, and performancefilter should be applied to actions other than edit and create. If the plus minus sign is not used in the filter configuration, the filter is applied to all actions.
Filter function:
A record of filtering and accessing operations for visitors and data
How to use:
One as a method of controller. The method name begins with filter.
Public Function Filteraccesscontrol ($filterChain)
{
echo "--->filteraccesscontrol";
$filterChain->run ();
}
The two define the opposing filter class, requiring extends Cfilter.
Cfilter
<?php/** * Cfilter is the base class of all filters.
* A filter can be applied before and the action is executed.
* It can modify the context which is the "action is" to run or decorate the "* action generates." * * Override {@link prefilter ()} to specify the filtering logic that should is 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 {/** * per
forms 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's on. */Public Function filter ($filterChain) {if ($this->prefilter ($filterChain)) {$filterChain->
Run ();
$this->postfilter ($filterChain);
}/** * initializes the filter.
* This is invoked after the filter properties are initialized * and before {@link prefilter} is called.
* 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's 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's on. */protected function Postfilter ($filterChain) {}}
The following examples illustrate the use of the 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
│&NBSP;&NBSP;├──TESTC ommand.php
│ └──testcommand.php~
├──components
│ ├──controller.php
│ & nbsp └──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 is executed
}
protected function Postfilter ($filterChain)
{
echo--& Gt 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
The above can see the specific execution flow of filter.
In the filters have-, +
The specific function is
+ means only action on these actions
-followed by the action name list. The expression excludes the outside.
If no-, + all the action that will be applied
More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design based on the YII framework.