自定义过滤器 2016.12.17

记录日志

Yii::log($content);

debug模式下,记录日志出现warning

修改config/web.php中的  $config['modules']['debug'] ,增加'allowedIPs' => ['127.0.0.1', ...]

使用自定义的过滤器

  • 在components下新建一个过滤器,继承于yii\base\ActionFilter
<?php

namespace app\components;

use Yii;
use yii\base\ActionFilter;

class ActionTimeFilter extends ActionFilter
{
    private $_startTime;

    public function beforeAction($action)
    {
        $this->_startTime = microtime(true);
        return parent::beforeAction($action);
    }

    public function afterAction($action, $result)
    {
        $time = microtime(true) - $this->_startTime;
        Yii::info("Action '{$action->uniqueId}' spent $time second.");
        return parent::afterAction($action, $result);
    }
}
  • 在contoller中使用自定义的过滤器
use app\components\ActionTimeFilter;
class YhlController extends Controller
{
  public function behaviors()
    {
        return [
            'access' => [
                'class' => ActionTimeFilter::className(),
                'only' => ['say-msg'],
            ],
        ];
    }
  ...
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,534评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,084评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,724评论 25 709
  • 日志 Yii提供了一个高度自定义化和高扩展性的日志框架。根据使用场景的不同,你可以很容易的对各种消息就行记录、过滤...
    柏树_Jeff阅读 12,610评论 1 11
  • 0. Install MacVim 1. 使用 Vundle 管理插件 1.1. Set up Vundle 1....
    43daf5f8181f阅读 3,106评论 0 0