Filter filter usage Analysis for YII framework, yiifilter_php tutorial

Source: Internet
Author: User
Tags getting started with php smarty template yii

The filter filter usage analysis of the YII framework, Yiifilter


This example describes the filter filter usage for the YII framework. Share to everyone for your reference, as follows:

First look at the official instructions for the document, what is the filter, the role of the filter, filter rules, filter definition method and so on.

Then a summary of the filter is made.

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 executes. For example, the access control filter will be executed to ensure that the user has been authenticated before the requested action is performed, and the performance filter can be used to measure the time taken by the controller to execute.

An action can have multiple filters. The filter execution order is the order in which they appear in the filter list. Filters can block actions and other filter executions in the back

A filter can be defined as a method of a controller class. The method name must begin with filter. For example, an existing Filteraccesscontrol method defines a filter named AccessControl. The filter method must be of the following structure:

Public Function Filteraccesscontrol ($filterChain) {  //calls $filterChain->run () to continue the execution of subsequent filters and actions. }

The $filterChain (filter chain) is an instance of Cfilterchain that represents the list of filters associated with the requested action. In the filter method, we can call $filterChain->run () to continue with 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 logic applied before the action is executed return    true;//If the action should not be executed, this returns false  }  protected function Postfilter ($filterChain)  {    //logic applied after action execution  }}

To apply a filter to an 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 code above specifies two filters: Postonly and Performancefilter. The Postonly filter is method-based (the corresponding filter method is defined in the Ccontroller), whereas the Performancefilter filter is Object-based. Path alias Application.filters.PerformanceFilter Specifies that the filter class file is Protected/filters/performancefilter. We use an array to configure the Performancefilter so that it can be used to initialize the property values of the Filter object. The value of the Unit property Performancefilter here will be initialized to second.

Using the plus minus sign, we can specify which actions should or should not apply the filter. In the code above, postonly should only be applied to the edit and create actions, and performancefilter should be applied to actions other than edit and create. If no minus sign is used in the filter configuration, this filter will be applied to all actions.

Filter function:

A record of filtering and accessing operations for visitors and data

How to use:

A method of being a controller. The method name begins with filter.

Public Function Filteraccesscontrol ($filterChain) {echo "--->filteraccesscontrol";  $filterChain->run ();}

The second definition of the opposing filter class 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, the action is to run or decorate the result, the * action generates. * * Override {@link prefilter ()} to specify the filtering logic this should be applied * before the action, and {@link P  Ostfilter ()} 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 C    Filter extends Ccomponent implements IFilter {/** * performs the filtering.    * The default implementation is to invoke {@link Prefilter} * and {@link Postfilter} which am meant to be overridden * Child classes. If a child class needs to override the This method, * Make sure it calls
  $filterChain->run()    * If the action should be executed.    * @param Cfilterchain $filterChain the filter chain. */Public Function filter ($filterChain) {if ($this->prefilter ($filterChain)) {$filterChain->run ()       ;     $this->postfilter ($filterChain);    }}/** * initializes the filter.    * This method was invoked after the filter properties was 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.    * @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 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
│├──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 Applie D before the action is executed         echo "-->myfilter-->pre";     return true; False if the action should not being 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

Above you can see the specific execution process of filter.

In the filters there are-, +
The specific function is
+ indicates that only action is applied to these actions
-a list of action names behind. Indicates exclusion.
If there is no-, + will apply all of the action

For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction Tutorial", " PHP String Usage Summary, "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

It is hoped that this article is helpful to the PHP program design based on YII framework.

Articles you may be interested in:

    • Introduction to some advanced usage of caching in the YII framework of PHP
    • In-depth parsing of PHP's caching capabilities in the YII framework
    • The use of view view in the Yii framework of PHP is advanced
    • A tutorial on model models in the YII framework of PHP
    • Controller controllers in the YII framework of PHP
    • Analysis of Yii Database Cache instance
    • How Yii turns on fragment caching
    • Attribute injection and method injection of component behavior in PHP's YII framework
    • A detailed approach to using behavioral behaviors in the PHP yii framework
    • Request and response usage of YII framework learning (based on chttprequest response)

http://www.bkjia.com/PHPjc/1117063.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117063.html techarticle The filter filter usage analysis of YII framework, Yiifilter This example describes the filter filter usage of YII framework. Share to everyone for your reference, as follows: First look at the official to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.