当前位置: 移动技术网 > IT编程>开发语言>PHP > YII Framework的filter过滤器用法分析

YII Framework的filter过滤器用法分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了yii framework的filter过滤器用法。分享给大家供大家参考,具体如下:

首先看官方给出的说明文档,什么是过滤器,过滤器的作用,过滤器的规则,过滤器的定义方法等等。

然后对过滤器进行一个总结。

过滤器是一段代码,可被配置在控制器动作执行之前或之后执行。例如, 访问控制过滤器将被执行以确保在执行请求的动作之前用户已通过身份验证;性能过滤器可用于测量控制器执行所用的时间。

一个动作可以有多个过滤器。过滤器执行顺序为它们出现在过滤器列表中的顺序。过滤器可以阻止动作及后面其他过滤器的执行

过滤器可以定义为一个控制器类的方法。方法名必须以 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 之外的动作。 如果过滤器配置中没有使用加减号,则此过滤器将被应用于所有动作。

过滤器功能:

用于对访问者和数据的过滤和对访问操作的记录

使用方法:

一作为controller的一个方法。方法名以filter开头。

public function filteraccesscontrol($filterchain)
{ 
echo "--->filteraccesscontrol";
  $filterchain->run();
}

二定义对立的filter类,要求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) 
  { 
  } 
}

下面举例说明两种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相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网