美文网首页
自定义过滤器 2016.12.17

自定义过滤器 2016.12.17

作者: yeqingwen | 来源:发表于2016-12-17 20:47 被阅读0次

记录日志

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'],
            ],
        ];
    }
  ...

相关文章

网友评论

      本文标题:自定义过滤器 2016.12.17

      本文链接:https://www.haomeiwen.com/subject/rvcamttx.html