首页 > 如何理解yii2的 Event?全局绑定,特别疑惑

如何理解yii2的 Event?全局绑定,特别疑惑

疑惑不解

这一段的内容 https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-events....,里面一直在不断的强调说事件Event的绑定:

php$foo = new Foo;

// this handler is a global function
$foo->on(Foo::EVENT_HELLO, 'function_name');

// this handler is an object method
$foo->on(Foo::EVENT_HELLO, [$object, 'methodName']);

// this handler is a static class method
$foo->on(Foo::EVENT_HELLO, ['app\components\Bar', 'methodName']);

// this handler is an anonymous function
$foo->on(Foo::EVENT_HELLO, function ($event) {
    // event handling logic
});

问题是

1)这一段代码是放在哪里?放在 public function actionXXX(){}里面吗?
2)我想让除admin的action之外的网站里面所有action在执行前都进行对会员是否登录,已登录会员是否已经创建了日志的检测,如何实现这个?


作为 __FresHmaN 的一个补充:



\yii\base\Event::on(\yii\web\User::className(), \yii\web\User::EVENT_BEFORE_LOGIN, function($event) { //your logic });
【热门文章】
【热门文章】