Events 在用户模型中,如何在登录后覆盖受保护的方法或在登录后使用事件?
我有一个实现Events 在用户模型中,如何在登录后覆盖受保护的方法或在登录后使用事件?,events,login,yii2,Events,Login,Yii2,我有一个实现IdentityInterface的用户模型 我想在用户通过cookie登录后执行一些操作。我试图覆盖User类中的afterLogin方法 yiisoft\yii2\web\User.php protected function afterLogin($identity, $cookieBased, $duration) { $this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([
IdentityInterface
的用户模型
我想在用户通过cookie登录后执行一些操作。我试图覆盖User
类中的afterLogin
方法
yiisoft\yii2\web\User.php
protected function afterLogin($identity, $cookieBased, $duration)
{
$this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([
'identity' => $identity,
'cookieBased' => $cookieBased,
'duration' => $duration,
]));
}
如何在用户模型中使用覆盖上述方法。或者在登录后如何使用事件
任何帮助都将不胜感激。谢谢
$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
});
这些例子摘自。这是一种处理事件的方法
我想您只需要在登录后使用User::EVENT\u即可。我最后在
bootstrap.php
中使用了EVENT\u。它可能在将来帮助某人
在bootstrap.php
文件中添加以下代码
use yii\base\Event;
use yii\web\User;
Event::on(User::className(), User::EVENT_AFTER_LOGIN, function() {
// your code
});
但问题仍然是如何在用户模型中覆盖afterLogin方法。这里也有几乎相同的问题。我在用户模型中添加了afterlogin函数,但它不起作用。我了解如何在登录后使用事件。但我的问题仍然是,我们不能在用户模型中覆盖afterLogin函数而不是使用event吗?