介绍
这篇文章主要讲解了“如何使用Yii里的特别行为ActionFilter”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习”如何使用Yii里的特别行为ActionFilter”吧!
<强>新建app \过滤器\ LoggingFilter继承Yii \基地\ ActionFilter 强>
LoggingFilter的功能:在指定请求的行动前后各记录一条日志
& lt; php ? namespace 应用\过滤器; use , yii \ \ ActionFilter基地; class LoggingFilter  extends ActionFilter { ,public function  beforeAction(动作) ,{ 父:才能beforeAction(行动);//,才能用do 一些东西 ,,printf(& # 39;却;能够is a logging for % s \ beforeAction. % & # 39;,, $ this→getActionId(行动),,PHP_EOL); return 才能;真实; ,} ,public function  afterAction(行动,美元,美元的结果) ,{ 父母才能::afterAction(行动,美元,美元的结果);//,才能用do 一些东西 ,,printf(& # 39;却;能够is a logging for % s \ afterAction. % & # 39;,, $ this→getActionId(行动),,PHP_EOL); return 才能;真实; ,} }
新建app \控制器\ SystemController
& lt; php ? namespace 应用\控制器; use 应用\ \ LoggingFilter过滤器; class SystemController  extends \ yii \ web \控制器 { ,public function 行为() ,{ 父母才能:行为(); return [才能 ,,& # 39;anchorAuth& # 39;,=祝辞,( ,,,& # 39;类# 39;,=祝辞,LoggingFilter::类名(), ,,,& # 39;只有# 39;,=祝辞,[& # 39;测试# 39;,,& # 39;test-one& # 39;],,//,仅对,& # 39;测试# 39;& # 39;test-one& # 39;,生效 ,,,& # 39;除了# 39;,=祝辞,[& # 39;test-one& # 39;],,//,排除,& # 39;test-one& # 39; ,,,, )才能; ,} ,public function  actionTestOne () ,{ ,,printf(& # 39;却;能够is a testing for % s % & # 39;,, $ this→getRoute (),, PHP_EOL); ,} ,public function  actionTestTwo () ,{ ,,printf(& # 39;却;能够is a testing for % s % & # 39;,, $ this→getRoute (),, PHP_EOL); ,} ,public function  actionTest () ,{ ,,printf(& # 39;却;能够is a testing for % s % & # 39;,, $ this→getRoute (),, PHP_EOL); ,} }
<>强测试强>
请求http://yii.test/index.php?r=系统/测试
却;能够is a logging \ beforeAction for 测试。 却,能够is a  testing for 系统/测试。 却,能够is a  logging for 测试\ afterAction。
请求http://yii.test/index.php?r=系统/测试1
却;能够is a testing for 系统/测试1。
请求http://yii.test/index.php?r=系统/测试2
却;能够is a testing for 系统/测试2。
<强>总结强>
Yii中的ActionFilter(过滤器)相当于Laravel中的中间件(中间件),beforeAction相当于前置中间件,afterAction相当于后置中间件。
感谢各位的阅读,以上就是“如何使用Yii里的特别行为ActionFilter”的内容了,经过本文的学习后,相信大家对如何使用Yii里的特别行为ActionFilter这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!