ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1. 创建过滤器目录 `app/components` 2. 创建过滤器文件:`ActionTimeFilter.php` ~~~ <?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; echo $time; return parent::afterAction($action,$result); } } ~~~ 3. 控制器中使用过滤器 ~~~ namespace app\controllers; use Yii; use app\models\Country; use app\models\CountrySearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use app\components\ActionTimeFilter;  --- 要引入命名空间 use yii\caching\FileCache; use app\models\User; /**  * CountryController implements the CRUD actions for Country model.  */ class CountryController extends Controller { public $defaultAction = 'index';     public function behaviors()     {         return [             'verbs' => [                 'class' => VerbFilter::className(),                 'actions' => [                     'delete' => ['post'],                 ],             ],             [                 'class'=>'app\components\ActionTimeFilter',                 'only'=>['index','test']             ],         ];     } ~~~