温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在YII Framework中使用filter过滤器

发布时间:2021-04-02 16:26:58 来源:亿速云 阅读:142 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关怎么在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 Framework中使用filter过滤器了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI