重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要講解了“如何使用Yii里的特別行為ActionFilter”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何使用Yii里的特別行為ActionFilter”吧!
新建 app\filters\LoggingFilter 繼承 yii\base\ActionFilter
LoggingFilter 的功能: 在指定請求的 action 前后各記錄一條日志
getActionId($action), PHP_EOL); return true; } public function afterAction($action, $result) { parent::afterAction($action, $result); // To do something printf('This is a logging for %s\afterAction.%s', $this->getActionId($action), PHP_EOL); return true; } }
新建 app\controllers\SystemController
[ 'class' => LoggingFilter::className(), 'only' => ['test', 'test-one'], // 僅對 'test'、'test-one' 生效 'except' => ['test-one'], // 排除 'test-one' ], ]; } public function actionTestOne() { printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL); } public function actionTestTwo() { printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL); } public function actionTest() { printf('This is a testing for %s.%s', $this->getRoute(), PHP_EOL); } }
測試
請求 http://yii.test/index.php?r=system/test
This is a logging for test\beforeAction. This is a testing for system/test. This is a logging for test\afterAction.
請求 http://yii.test/index.php?r=system/test-one
This is a testing for system/test-one.
請求 http://yii.test/index.php?r=system/test-two
This is a testing for system/test-two.
總結
Yii 中的 ActionFilter(過濾器)相當于 Laravel 中的 Middleware(中間件),beforeAction 相當于前置中間件,afterAction 相當于后置中間件。
感謝各位的閱讀,以上就是“如何使用Yii里的特別行為ActionFilter”的內容了,經過本文的學習后,相信大家對如何使用Yii里的特別行為ActionFilter這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創新互聯網站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!