
這(zhe)篇文章主要給大家介紹了關(guān)于?Yii中特殊行為Action??Filte??r的特殊使用方法,文中通過(guò)示例代碼介紹的用方非常詳細,對大家的法示學(xué)ヽ(′▽?zhuān)?/習或者工作具有一定的參考學(xué)習價(jià)值,需要的特殊朋友們下面隨著(zhù)小編來(lái)一起學(xué)習學(xué)習吧
#FormatStrong??ID_0#
LoggingFil(′?_?`)ter 的功能: 在指定請求的 action 前后各記錄一條日志
<?php
namespace app\f(?Д?)ilters;
use yii\b??ase\ActionFilter;
class LoggingFilter extends ActionFilter
{
public function beforeAction($action)
{
parent::beforeAction($action);
// To do somヾ(′▽?zhuān)??ething
printf('T??his is a loggiヽ(′▽?zhuān)?ノng for %s\beforeAc??tion.%s', $this->getActionId($action), PHP_EOL);
retヽ(′ー`)ノurn true;
}
public function afterA(?⊿?)ction($action(′▽?zhuān)?), $result)
{
parent::afterAction($act(′?`*)ion, $result);
// To do something
print(╬ ò﹏ó)f('Th??is is a logging for %s\a( ?ω?)fterAction.%s', $this->getActionId($acti(T_T)on), PHP_EOL);
return true;
}
}
新建 app\controllers\SystemController
namespace app\controllers;
use app\filters\(′?`*)LoggingFilter;
class SystemController extends \yii\web\Controller
{
public function behaviors()
{
parent::behaviors();
return [
'anchorAuth' => [
'class' =&(′▽?zhuān)?)gt; LoggingFilter::className(),
'only' => ['test', 'teヾ(′?`)?st-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('Thi??s is a testヾ(′▽?zhuān)??i??ng for %s.%s??', $this->getRoute(), PHP_EOL);
}
public function actionTest??()
{
printf('This is a te??sting for %s.%s', $this->getRoute(), PHP_E??OL);(°□°)
}
}
#Format??StrongIヽ(′▽?zhuān)?ノD??_1??#
請求 http://yii.test/index.php???r=system/test
Thi(???)s is a logging for test\beforeAction.
This is a tes??ting for system/test.
This is a logging for test\af(′?`)ter??Action??.
請求 http://yii.test/index.php?r=system/test-two
This is a testing for system/test-two.
Yii中特殊行為ActionFilter的使用方法示例
#For??matStrongID_2#
Yii 中的 ActionFilter(過(guò)濾器)相當于 Laravel 中的 Middleware(中間件),beforeAction 相當于前置中間(jian)件,法示afterAc(′?_?`)tion 相當于后置??中間件。??特殊
來(lái)源:腳本之家
法示鏈接:https://www.jb51.net/??article/197626.htm
法示