<?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"; } }
?r=site/print
--->filterAccessControl--->print action
?r=site/create
-->MyFilter-->pre--->create action-->MyFilter-->post
public function filters() { return array( 'AccessControl - create', array( 'application.filters.MyFilter + create,print', ), ); }
?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常见数据库操作技巧汇总》