Yii中特殊行为ActionFilter的使用方法示例


Posted in PHP onOctober 18, 2020

新建 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学习之PHP运算符
Oct 09 PHP
我的论坛源代码(一)
Oct 09 PHP
PHP采集相关教程之一 CURL函数库
Feb 15 PHP
PHP的范围解析操作符(::)的含义分析说明
Jul 03 PHP
ajax返回值中有回车换行、空格的解决方法分享
Oct 24 PHP
PHP输入流php://input实例讲解
Dec 22 PHP
微信网页授权(OAuth2.0) PHP 源码简单实现
Aug 29 PHP
yii2 数据库读写分离配置示例
Feb 10 PHP
PHP实现获取url地址中顶级域名的方法示例
Jun 05 PHP
PHP字符串与数组处理函数用法小结
Jan 07 PHP
Centos7安装swoole扩展操作示例
Mar 26 PHP
PHP+Mysql分布式事务与解决方案深入理解
Feb 27 PHP
PHP 99乘法表的几种实现代码
Oct 13 #PHP
php7连接MySQL实现简易查询程序的方法
Oct 13 #PHP
laravel7学习之无限级分类的最新实现方法
Sep 30 #PHP
如何利用PHP实现上传图片功能详解
Sep 24 #PHP
JS中彻底删除JSON对象组成的数组中的元素
Sep 22 #PHP
phpstudy隐藏index.php的方法
Sep 21 #PHP
如何在Laravel之外使用illuminate组件详解
Sep 20 #PHP
You might like
全文搜索和替换
2006/10/09 PHP
利用PHP和AJAX创建RSS聚合器的代码
2007/03/13 PHP
php 大数据量及海量数据处理算法总结
2011/05/07 PHP
PHP可变函数的使用详解
2013/06/14 PHP
页面乱码问题的根源及其分析
2013/08/09 PHP
不错的新闻标题颜色效果
2006/12/10 Javascript
js浮动图片的动态效果
2013/07/10 Javascript
jquery 鼠标滑动显示详情应用示例
2014/01/24 Javascript
js与C#进行时间戳转换
2014/11/14 Javascript
js改变Iframe中Src的方法
2015/05/05 Javascript
Bootstrap布局组件教程之Bootstrap下拉菜单
2016/06/12 Javascript
AngularJS入门教程之模块化操作用法示例
2016/11/02 Javascript
原生JS下拉加载插件分享
2016/12/26 Javascript
Angular实现较为复杂的表格过滤,删除功能示例
2017/12/23 Javascript
Node.js + express实现上传大文件的方法分析【图片、文本文件】
2019/03/14 Javascript
vue中获取滚动table的可视页面宽度调整表头与列对齐(每列宽度不都相同)
2019/08/17 Javascript
layui关闭层级、简单监听的实例
2019/09/06 Javascript
Element Carousel 走马灯的具体实现
2020/07/26 Javascript
python赋值操作方法分享
2013/03/23 Python
Python实现程序的单一实例用法分析
2015/06/03 Python
python中reader的next用法
2018/07/24 Python
TensorFlow打印tensor值的实现方法
2018/07/27 Python
Python实现查找字符串数组最长公共前缀示例
2019/03/27 Python
详解numpy矩阵的创建与数据类型
2019/10/18 Python
python ftplib模块使用代码实例
2019/12/31 Python
python标准库OS模块函数列表与实例全解
2020/03/10 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2020/08/07 Python
德国圣伯纳德草药屋:Kräuterhaus Sanct Bernhard(有中文站)
2018/08/05 全球购物
斯巴达比赛商店:Spartan Race
2019/01/08 全球购物
查找廉价航班和发现新目的地:Kiwi.com
2019/02/25 全球购物
甜品蛋糕店创业计划书范文
2014/02/06 职场文书
五水共治一句话承诺
2014/05/30 职场文书
php png失真的原因及解决办法
2021/10/24 PHP
浅谈JavaScript作用域
2021/12/06 Javascript
MYSQL如何查看操作日志详解
2022/05/30 MySQL
Win11 Build 25179预览版发布(附更新内容+ISO官方镜像下载)
2022/08/14 数码科技