麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > PHP > 正文

YII Framework的filter過濾器用法分析

2024-05-04 23:44:43
字體:
供稿:網(wǎng)友
這篇文章主要介紹了YII Framework的filter過濾器用法,結(jié)合實(shí)例形式分析了filter過濾器的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
 

本文實(shí)例講述了YII Framework的filter過濾器用法。分享給大家供大家參考,具體如下:

首先看官方給出的說明文檔,什么是過濾器,過濾器的作用,過濾器的規(guī)則,過濾器的定義方法等等。

然后對過濾器進(jìn)行一個總結(jié)。

http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller

過濾器是一段代碼,可被配置在控制器動作執(zhí)行之前或之后執(zhí)行。例如, 訪問控制過濾器將被執(zhí)行以確保在執(zhí)行請求的動作之前用戶已通過身份驗(yàn)證;性能過濾器可用于測量控制器執(zhí)行所用的時間。

一個動作可以有多個過濾器。過濾器執(zhí)行順序?yàn)樗鼈兂霈F(xiàn)在過濾器列表中的順序。過濾器可以阻止動作及后面其他過濾器的執(zhí)行

過濾器可以定義為一個控制器類的方法。方法名必須以 filter 開頭。例如,現(xiàn)有的 filterAccessControl 方法定義了一個名為 accessControl 的過濾器。 過濾器方法必須為如下結(jié)構(gòu):

public function filterAccessControl($filterChain){  // 調(diào)用 $filterChain->run() 以繼續(xù)后續(xù)過濾器與動作的執(zhí)行。}

其中的 $filterChain (過濾器鏈)是一個 CFilterChain 的實(shí)例,代表與所請求動作相關(guān)的過濾器列表。在過濾器方法中, 我們可以調(diào)用 $filterChain->run() 以繼續(xù)執(zhí)行后續(xù)過濾器和動作。

過濾器也可以是一個 CFilter 或其子類的實(shí)例。如下代碼定義了一個新的過濾器類:

class PerformanceFilter extends CFilter{  protected function preFilter($filterChain)  {    // 動作被執(zhí)行之前應(yīng)用的邏輯    return true; // 如果動作不應(yīng)被執(zhí)行,此處返回 false  }  protected function postFilter($filterChain)  {    // 動作執(zhí)行之后應(yīng)用的邏輯  }}

要對動作應(yīng)用過濾器,我們需要覆蓋 CController::filters() 方法。此方法應(yīng)返回一個過濾器配置數(shù)組。例如:

class PostController extends CController{  ......  public function filters()  {    return array(      'postOnly + edit, create',      array(        'application.filters.PerformanceFilter - edit, create',        'unit'=>'second',      ),    );  }}

上述代碼指定了兩個過濾器: postOnly 和 PerformanceFilter。 postOnly 過濾器是基于方法的(相應(yīng)的過濾器方法已在 CController 中定義); 而 performanceFilter 過濾器是基于對象的。路徑別名application.filters.PerformanceFilter 指定過濾器類文件是protected/filters/PerformanceFilter。我們使用一個數(shù)組配置 PerformanceFilter ,這樣它就可被用于初始化過濾器對象的屬性值。此處 PerformanceFilter 的 unit 屬性值將被初始為 second。

使用加減號,我們可指定哪些動作應(yīng)該或不應(yīng)該應(yīng)用過濾器。上述代碼中, postOnly 應(yīng)只被應(yīng)用于 edit 和create 動作,而 PerformanceFilter 應(yīng)被應(yīng)用于 除了 edit 和 create 之外的動作。 如果過濾器配置中沒有使用加減號,則此過濾器將被應(yīng)用于所有動作。

過濾器功能:

用于對訪問者和數(shù)據(jù)的過濾和對訪問操作的記錄

使用方法:

一作為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 <[email protected]>  * @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規(guī)則的使用:

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的具體執(zhí)行流程。

在filters中有-、+
具體功能是
+表示僅僅作用于這一些action
-后邊跟action名稱列表。表示排除在外。
如果沒有-、+則會應(yīng)用的所有的action



注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 中国国语毛片免费观看视频 | 国产成人在线观看免费 | 精品久久久久久久久亚洲 | 免费一级高清毛片 | 玖玖精品视频在线 | 欧美亚洲国产一区二区三区 | 亚洲精品一区国产精品丝瓜 | 激情午夜天 | 在线亚洲欧美 | 久久国产精品二区 | www.热| 特级西西444www大精品视频免费看 | av在线免费观看中文字幕 | 久久9999久久 | 在线看一区二区三区 | 免费a级作爱片免费观看欧洲 | 国产在线观看91精品 | 国产精品成人亚洲一区二区 | 欧美日韩亚洲国产精品 | 黄色片免费看网站 | 在线成人精品视频 | asian附近女人裸体pics | 娇妻被各种姿势c到高潮小说 | 国产一区二区视频网站 | 欧美精品免费一区二区三区 | 国产成人在线观看免费网站 | 国产午夜精品在线 | 久久免费精品视频 | 欧美一级免费视频 | 午夜精品福利视频 | 久久色伦理资源站 | 毛片在线免费视频 | 国产亚洲精品久久久久久久软件 | 羞羞视频免费观看网站 | 毛片在线视频免费观看 | 福利在线免费 | 成人免费网站在线观看视频 | 国产成人精品自拍视频 | 在线播放免费播放av片 | 一级美女大片 | 91av在线免费观看 |