多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
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']             ],         ];     } ~~~