《PHP學習:YII Framework的filter過濾器用法分析》要點:
本文介紹了PHP學習:YII Framework的filter過濾器用法分析,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
PHP應用本文實例講述了YII Framework的filter過濾器用法.分享給大家供大家參考,具體如下:
PHP應用首先看官方給出的說明文檔,什么是過濾器,過濾器的作用,過濾器的規則,過濾器的定義辦法等等.
PHP應用然后對過濾器進行一個總結.
PHP應用http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller
PHP應用過濾器是一段代碼,可被配置在控制器動作執行之前或之后執行.例如, 訪問控制過濾器將被執行以確保在執行哀求的動作之前用戶已通過身份驗證;性能過濾器可用于測量控制器執行所用的時間.
PHP應用一個動作可以有多個過濾器.過濾器執行順序為它們出現在過濾器列表中的順序.過濾器可以阻止動作及后面其他過濾器的執行
PHP應用過濾器可以定義為一個控制器類的辦法.辦法名必須以 filter 開頭.例如,現有的 filterAccessControl 辦法定義了一個名為 accessControl 的過濾器. 過濾器辦法必須為如下結構:
PHP應用
public function filterAccessControl($filterChain)
{
// 調用 $filterChain->run() 以繼續后續過濾器與動作的執行.
}
PHP應用其中的 $filterChain (過濾器鏈)是一個 CFilterChain 的實例,代表與所哀求動作相關的過濾器列表.在過濾器方法中, 我們可以調用 $filterChain->run() 以繼續執行后續過濾器和動作.
PHP應用過濾器也可以是一個 CFilter 或其子類的實例.如下代碼定義了一個新的過濾器類:
PHP應用
class PerformanceFilter extends CFilter
{
protected function preFilter($filterChain)
{
// 動作被執行之前應用的邏輯
return true; // 如果動作不應被執行,此處返回 false
}
protected function postFilter($filterChain)
{
// 動作執行之后應用的邏輯
}
}
PHP應用要對動作應用過濾器,我們需要覆蓋 CController::filters() 辦法.此辦法應返回一個過濾器配置數組.例如:
PHP應用
class PostController extends CController
{
......
public function filters()
{
return array(
'postOnly + edit, create',
array(
'application.filters.PerformanceFilter - edit, create',
'unit'=>'second',
),
);
}
}
PHP應用上述代碼指定了兩個過濾器: postOnly 和 PerformanceFilter. postOnly 過濾器是基于辦法的(相應的過濾器辦法已在 CController 中定義); 而 performanceFilter 過濾器是基于對象的.路徑別名application.filters.PerformanceFilter 指定過濾器類文件是protected/filters/PerformanceFilter.我們使用一個數組配置 PerformanceFilter ,這樣它就可被用于初始化過濾器對象的屬性值.此處 PerformanceFilter 的 unit 屬性值將被初始為 second.
PHP應用使用加減號,我們可指定哪些動作應該或不應該應用過濾器.上述代碼中, postOnly 應只被應用于 edit 和create 動作,而 PerformanceFilter 應被應用于 除了 edit 和 create 之外的動作. 如果過濾器配置中沒有使用加減號,則此過濾器將被應用于所有動作.
PHP應用過濾器功能:
PHP應用用于對拜訪者和數據的過濾和對拜訪操作的記錄
PHP應用使用辦法:
PHP應用一作為controller的一個辦法.辦法名以filter開頭.
PHP應用
public function filterAccessControl($filterChain)
{
echo "--->filterAccessControl";
$filterChain->run();
}
PHP應用二定義對立的filter類,要求extends CFilter.
PHP應用CFilter
PHP應用
<?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)
{
}
}
PHP應用下面舉例說明兩種filter規則的使用:
PHP應用SiteController.php
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";
}
PHP應用/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應用
<?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";
}
}
PHP應用http://www.localyii.com/testwebap/index.php?r=site/print
PHP應用--->filterAccessControl--->print action
PHP應用http://www.localyii.com/testwebap/index.php?r=site/create
PHP應用-->MyFilter-->pre--->create action-->MyFilter-->post
PHP應用
public function filters()
{
return array(
'AccessControl - create',
array(
'application.filters.MyFilter + create,print',
),
);
}
PHP應用http://www.localyii.com/testwebap/index.php?r=site/print
--->filterAccessControl-->MyFilter-->pre--->print action-->MyFilter-->post
PHP應用以上可以看到filter的具體執行流程.
PHP應用在filters中有-、+
具體功能是
+表示僅僅作用于這一些action
-后邊跟action名稱列表.表示排除在外.
如果沒有-、+則會應用的所有的action
PHP應用更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP應用希望本文所述對大家基于Yii框架的PHP程序設計有所贊助.
維易PHP培訓學院每天發布《PHP學習:YII Framework的filter過濾器用法分析》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7011.html