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']
????????????],
????????];
????}
~~~
- 目錄
- 配置
- 簡介
- 別名
- gii
- 配置項
- 模型
- 簡介
- 增刪改查
- AR和model
- 模型事件
- 場景
- query查詢
- 增刪改
- AR查詢器
- 模型關系定義
- AR模型連表查詢
- fields
- where拼接
- 模塊
- 創建模塊
- 控制器
- 表單
- 跳轉
- 響應
- 驗證器
- Action
- 組件
- url
- 分頁
- 驗證碼
- 緩存
- 文件上傳
- 預啟動組件
- 事件
- 自定義組件
- redis
- 日志
- 行為
- cookie和session
- 基礎知識
- 創建一個類
- 配置一個類
- object基類
- component組件類特性
- phpstorm無法更改php等級
- url地址美化
- 過濾器
- 請求處理
- 請求組件
- 響應組件
- header
- 用戶登錄
- 實現IdentityInterface接口
- 登錄
- 自動檢測登錄
- 獲取用戶信息
- 訪問行為追蹤
- phpstorm+postman斷點調試