YII Framework的filter過(guò)濾器用法分析
本文實(shí)例講述了YII Framework的filter過(guò)濾器用法。分享給大家供大家參考,具體如下:
首先看官方給出的說(shuō)明文檔,什么是過(guò)濾器,過(guò)濾器的作用,過(guò)濾器的規(guī)則,過(guò)濾器的定義方法等等。
然后對(duì)過(guò)濾器進(jìn)行一個(gè)總結(jié)。
http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller
過(guò)濾器是一段代碼,可被配置在控制器動(dòng)作執(zhí)行之前或之后執(zhí)行。例如, 訪問(wèn)控制過(guò)濾器將被執(zhí)行以確保在執(zhí)行請(qǐng)求的動(dòng)作之前用戶已通過(guò)身份驗(yàn)證;性能過(guò)濾器可用于測(cè)量控制器執(zhí)行所用的時(shí)間。
一個(gè)動(dòng)作可以有多個(gè)過(guò)濾器。過(guò)濾器執(zhí)行順序?yàn)樗鼈兂霈F(xiàn)在過(guò)濾器列表中的順序。過(guò)濾器可以阻止動(dòng)作及后面其他過(guò)濾器的執(zhí)行
過(guò)濾器可以定義為一個(gè)控制器類(lèi)的方法。方法名必須以 filter 開(kāi)頭。例如,現(xiàn)有的 filterAccessControl 方法定義了一個(gè)名為 accessControl 的過(guò)濾器。 過(guò)濾器方法必須為如下結(jié)構(gòu):
public function filterAccessControl($filterChain)
{
// 調(diào)用 $filterChain->run() 以繼續(xù)后續(xù)過(guò)濾器與動(dòng)作的執(zhí)行。
}
其中的 $filterChain (過(guò)濾器鏈)是一個(gè) CFilterChain 的實(shí)例,代表與所請(qǐng)求動(dòng)作相關(guān)的過(guò)濾器列表。在過(guò)濾器方法中, 我們可以調(diào)用 $filterChain->run() 以繼續(xù)執(zhí)行后續(xù)過(guò)濾器和動(dòng)作。
過(guò)濾器也可以是一個(gè) CFilter 或其子類(lèi)的實(shí)例。如下代碼定義了一個(gè)新的過(guò)濾器類(lèi):
class PerformanceFilter extends CFilter
{
protected function preFilter($filterChain)
{
// 動(dòng)作被執(zhí)行之前應(yīng)用的邏輯
return true; // 如果動(dòng)作不應(yīng)被執(zhí)行,此處返回 false
}
protected function postFilter($filterChain)
{
// 動(dòng)作執(zhí)行之后應(yīng)用的邏輯
}
}
要對(duì)動(dòng)作應(yīng)用過(guò)濾器,我們需要覆蓋 CController::filters() 方法。此方法應(yīng)返回一個(gè)過(guò)濾器配置數(shù)組。例如:
class PostController extends CController
{
......
public function filters()
{
return array(
'postOnly + edit, create',
array(
'application.filters.PerformanceFilter - edit, create',
'unit'=>'second',
),
);
}
}
上述代碼指定了兩個(gè)過(guò)濾器: postOnly 和 PerformanceFilter。 postOnly 過(guò)濾器是基于方法的(相應(yīng)的過(guò)濾器方法已在 CController 中定義); 而 performanceFilter 過(guò)濾器是基于對(duì)象的。路徑別名application.filters.PerformanceFilter 指定過(guò)濾器類(lèi)文件是protected/filters/PerformanceFilter。我們使用一個(gè)數(shù)組配置 PerformanceFilter ,這樣它就可被用于初始化過(guò)濾器對(duì)象的屬性值。此處 PerformanceFilter 的 unit 屬性值將被初始為 second。
使用加減號(hào),我們可指定哪些動(dòng)作應(yīng)該或不應(yīng)該應(yīng)用過(guò)濾器。上述代碼中, postOnly 應(yīng)只被應(yīng)用于 edit 和create 動(dòng)作,而 PerformanceFilter 應(yīng)被應(yīng)用于 除了 edit 和 create 之外的動(dòng)作。 如果過(guò)濾器配置中沒(méi)有使用加減號(hào),則此過(guò)濾器將被應(yīng)用于所有動(dòng)作。
過(guò)濾器功能:
用于對(duì)訪問(wèn)者和數(shù)據(jù)的過(guò)濾和對(duì)訪問(wèn)操作的記錄
使用方法:
一作為controller的一個(gè)方法。方法名以filter開(kāi)頭。
public function filterAccessControl($filterChain)
{
echo "--->filterAccessControl";
$filterChain->run();
}
二定義對(duì)立的filter類(lèi),要求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)
{
}
}
下面舉例說(shuō)明兩種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名稱(chēng)列表。表示排除在外。
如果沒(méi)有-、+則會(huì)應(yīng)用的所有的action
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- 詳解在PHP的Yii框架中使用行為Behaviors的方法
- YII框架行為behaviors用法示例
- YII2.0框架行為(Behavior)深入詳解
- Yii控制器中filter過(guò)濾器用法分析
- Yii凈化器CHtmlPurifier用法示例(過(guò)濾不良代碼)
- PHP的Yii框架中過(guò)濾器相關(guān)的使用總結(jié)
- 詳解PHP的Yii框架中的Controller控制器
- Yii2創(chuàng)建控制器(createController)方法詳解
- yii2控制器Controller Ajax操作示例
- Yii2設(shè)置默認(rèn)控制器的兩種方法
- yii2 在控制器中驗(yàn)證請(qǐng)求參數(shù)的使用方法
- Yii2.0框架behaviors方法使用實(shí)例分析
相關(guān)文章
bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解
下面小編就為大家分享一篇bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
thinkphp 3.2框架視圖模型 實(shí)例視圖查詢結(jié)果的二維數(shù)組合并操作示例
這篇文章主要介紹了thinkphp 3.2框架視圖模型 實(shí)例視圖查詢結(jié)果的二維數(shù)組合并操作,結(jié)合實(shí)例形式分析了thinkPHP3.2針對(duì)視圖查詢結(jié)果的數(shù)組合并相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
eWebEditor v3.8 商業(yè)完整版 (PHP)
eWebEditor v3.8 商業(yè)完整版 (PHP)...2006-12-12
實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫(kù)方法
這篇文章主要介紹了實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫(kù)方法的相關(guān)資料,需要的朋友可以參考下2016-03-03
php腳本運(yùn)行時(shí)的超時(shí)機(jī)制詳解
在我們平常的開(kāi)發(fā)中,也許曾經(jīng)都遇到過(guò)PHP腳本運(yùn)行超時(shí)的情況,當(dāng)遇到這種情況我們經(jīng)常會(huì)通過(guò)使用 set_time_limit(非安全模式),或修改配置文件并重啟服務(wù)器,或者修改程序減少程序的執(zhí)行時(shí)間,使其在允許的范圍之內(nèi),以解決此問(wèn)題。2016-02-02
PHP獲取當(dāng)前URL路徑的處理方法(適用于多條件篩選列表)
下面小編就為大家?guī)?lái)一篇PHP獲取當(dāng)前URL路徑的處理方法(適用于多條件篩選列表)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例
這篇文章主要介紹了TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作,結(jié)合實(shí)例形式分析了MongoDB擴(kuò)展的基本安裝、配置、模型操作以及使用Push操作實(shí)現(xiàn)的數(shù)據(jù)添加、更新等方法,需要的朋友可以參考下2018-09-09

