一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - Yii中特殊行為ActionFilter的使用方法示例

Yii中特殊行為ActionFilter的使用方法示例

2021-10-27 13:44琯琯 PHP教程

這篇文章主要給大家介紹了關于Yii中特殊行為ActionFilter的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

新建 app\filters\LoggingFilter 繼承 yii\base\ActionFilter

LoggingFilter 的功能: 在指定請求的 action 前后各記錄一條日志

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 
namespace app\filters;
 
use yii\base\ActionFilter;
 
class LoggingFilter extends ActionFilter
{
 public function beforeAction($action)
 {
  parent::beforeAction($action);
 
  // To do something
  printf('This is a logging for %s\beforeAction.%s', $this->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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
namespace app\controllers;
 
use app\filters\LoggingFilter;
 
class SystemController extends \yii\web\Controller
{
 public function behaviors()
 {
  parent::behaviors();
 
  return [
   'anchorAuth' => [
    '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

?
1
2
3
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

?
1
This is a testing for system/test-one.

請求 http://yii.test/index.php?r=system/test-two

?
1
This is a testing for system/test-two.

總結

Yii 中的 ActionFilter(過濾器)相當于 Laravel 中的 Middleware(中間件),beforeAction 相當于前置中間件,afterAction 相當于后置中間件。

到此這篇關于Yii中特殊行為ActionFilter使用的文章就介紹到這了,更多相關Yii特殊行為ActionFilter使用內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://segmentfault.com/a/1190000037480693

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色哟哟在线观看 | 亚洲AV久久久久久久无码 | 国产成人精品三级在线 | 奇米7777第四色 | 国产成人久久久精品一区二区三区 | 欧美一级专区免费大片 | 99re这里只有精品视频在线观看 | 成人依依网 | 国产第一综合另类色区奇米 | 欧美 亚洲 综合 卡通 另类 区 | 免费观看的毛片 | 欧美日韩亚洲国内综合网俺 | 高清国产欧美一v精品 | 国产欧美亚洲精品第一页青草 | hd最新国产人妖ts视频 | а天堂中文最新版在线 | 九九热只有精品 | 18捆绑调教在线高清 | 美女福利网站 | 倩女还魂在线观看完整版免费 | 午夜伦伦电影理论片大片 | 99re7在线精品免费视频 | 日本视频二区 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 五月婷婷俺来也 | 四虎导航| 九色PORNY蝌蚪视频首页 | b片在线观看| 99久久国产综合精品女小说 | 欧美亚洲激情在线 | www.羞羞答答| 青青热久麻豆精品视频在线观看 | 亚洲冬月枫中文字幕在线看 | 无码爽死成人777在线观看网站 | 色小妹在线 | 四虎影视地址 | 久久成人精品免费播放 | 小sao货水好多真紧h的视频 | 国产二区精品视频 | 国产精品视频自拍 | 美女在线看永久免费网址 |