I am trying to log every controller's actions by same code:
public function afterAction($action, $result)
{
\Yii::$app->logger->write(0, $action->controller->id, $action->id);
return parent::afterAction($action, $result);
}
But, I don't want redeclare this method on every controller, and I don't want to use some BaseController with same method. I know, base/Controller has AfterAction Event, but how to log controller actions, using his event handler ?
You can create a Class-Level Event Handler in the boostrap process like this (most likely in the web.php configuration file, which holds the configuration for the application object):
use yii\base\ActionEvent;
use yii\base\Controller;
use yii\base\Event;
$config = [
...
'bootstrap' => [
...
function () {
Event::on(Controller::class, Controller::EVENT_AFTER_ACTION, function (ActionEvent $event) {
Yii::info('Called controller/action: ' . $event->action->id . '/' . $event->action->controller->id);
});
},
...
],
...
];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.