Yii中特殊行为ActionFilter的使用方法示例
时间:2022-01-07
来源:互联网
标签:
这篇文章主要给大家介绍了关于Yii中特殊行为ActionFilter的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
新建 app\filters\LoggingFilter 继承 yii\base\ActionFilter
LoggingFilter 的功能: 在指定请求的 action 前后各记录一条日志
<?php
namespace app\filters;
use yii\base\ActionFilter;
class LoggingFilter extends ActionFilter
{
public function beforeAction($action)
{
parent::beforeAction($action);
// To do something
printf('This is a logging for %s\beforeAction.%s', $this->getActionId($action), PHP_EOL);
return true;
}
public function afterAction($action, $result)
{
parent::afterAction($action, $result);
// To do something
printf('This is a logging for %s\afterAction.%s', $this->getActionId($action), PHP_EOL);
return true;
}
}新建 app\controllers\SystemController
<?php
namespace app\controllers;
use app\filters\LoggingFilter;
class SystemController extends \yii\web\Controller
{
public function behaviors()
{
parent::behaviors();
return [
'anchorAuth' => [
'class' => LoggingFilter::className(),
'only' => ['test', 'test-one'], // 仅对 'test'、'test-one' 生效
'except' => ['test-one'], // 排除 'test-one'
],
];
}
public function actionTestOne()
{
printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL);
}
public function actionTestTwo()
{
printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL);
}
public function actionTest()
{
printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL);
}
}测试
请求 http://yii.test/index.php?r=system/test
This is a logging for test\beforeAction. This is a testing for system/test. This is a logging for test\afterAction.
请求 http://yii.test/index.php?r=system/test-one
This is a testing for system/test-one.
请求 http://yii.test/index.php?r=system/test-two
This is a testing for system/test-two.
总结
Yii 中的 ActionFilter(过滤器)相当于 Laravel 中的 Middleware(中间件),beforeAction 相当于前置中间件,afterAction 相当于后置中间件。
到此这篇关于Yii中特殊行为ActionFilter使用的文章就介绍到这了,更多相关Yii特殊行为ActionFilter使用内容请搜索PHP爱好者以前的文章或继续浏览下面的相关文章希望大家以后多多支持PHP爱好者!
-
核芯显卡是什么意思?核芯显卡和独立显卡有什么区别? 时间:2025-12-19 -
什么是算术逻辑单元ALU 算术逻辑单元的功能和结构 时间:2025-12-19 -
什么是视觉识别色差检测 视觉识别色差检测的原理、技术特点、应用及常用工具 时间:2025-12-19 -
什么是流量控制 流量控制和拥塞控制的区别 时间:2025-12-19 -
GPU虚拟化是什么意思 GPU虚拟化有哪三种方法 时间:2025-12-19 -
独显是什么意思 独显和集显的区别 时间:2025-12-19
今日更新
-
币安现货交易极端低价下止损订单如何受影响
阅读:18
-
qq网页版手机登录入口-手机QQ网页版一键登录入口
阅读:18
-
喵趣漫画App官方最新版本下载-喵趣漫画App正版安装包下载入口
阅读:18
-
mc网页版手机入口-我的世界网页版手机直进
阅读:18
-
喵趣漫画App官方最新版本下载-喵趣漫画App正版安装包下载入口
阅读:18
-
币安理财产品如何精准匹配用户风险偏好
阅读:18
-
欧洲人的梗是什么梗?揭秘欧式幽默背后的文化差异与搞笑名场面!
阅读:18
-
歪歪漫画首页登陆入口-歪歪漫画官网登录页面
阅读:18
-
欧洲人非洲人是什么梗?揭秘欧非血统爆笑网络梗,看完秒懂!
阅读:18
-
币安系统故障致交易延误 官方补偿方案详解
阅读:18










