How is YII2 written and configured before and after all action requests?

this is my ActionTimeFilter, so how can I configure it in web.php to enable every action to pass through this filter? Wait online. I"ve been in a hurry all day. Is it so good?

<?php
namespace app\filters;

use Yii;
use yii\base\Action;
use yii\base\ActionFilter;

class ActionTimeFilter extends ActionFilter
{
    private $_startTime;

    public function beforeAction($action)
    {
        Yii::info("2222222222222");
        $this->_startTime = microtime(true);
        return array(
            "dd"=>2
        );
        return parent::beforeAction($action);
//        return parent::beforeAction($action);
    }

    public function afterAction($action, $result)
    {
        $time = microtime(true) - $this->_startTime;
        Yii::debug("Action "{$action->uniqueId}" spent $time second.");
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return array(
            "dd"=>2
        );
//        $time = microtime(true) - $this->_startTime;
//        Yii::debug("Action "{$action->uniqueId}" spent $time second.");
//        Yii::$app->response->format=Response::FORMAT_JSON;
        return parent::afterAction($action, $result);
    }
}
Mar.17,2021

you need to build a general-purpose controller first

use yii\web\Controller;

class CommonController extend Controller {
  public function behaviors() {
    return [
      'timeFilter' => 'app\filters\ActionTimeFilter',
    ];
  }
}

other controllers can inherit this


available for parent test:
class BaseActiveController extends \yii\rest\ActiveController
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors['timeFilter'] = [
            'class' => 'app\filters\ActionTimeFilter',
            'except'=>['test']
        ];

        return $behaviors;
    }
}
it is worth noting that behaviors () needs to inherit the content returned by the parent class behaviors () method, otherwise the parent class behaviors () will be completely overwritten.

inject a behavior into the app instance and bind the handler of the corresponding event in the behavior.

actual combat:
1, configuration file
clipboard.png

2,
clipboard.png

:

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be7e00-3189b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be7e00-3189b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?