Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Events 在用户模型中,如何在登录后覆盖受保护的方法或在登录后使用事件?_Events_Login_Yii2 - Fatal编程技术网

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吗?