我正在尝试确定在事件侦听器中获取经过身份验证的用户信息的正确方法。
我总是可以使用\Auth:user()
,它似乎可以工作,但是如果我将监听器排队,我认为它不会工作。
是否需要将经过身份验证的用户作为每个事件的属性添加到事件类中,还是有其他方法可以执行?
最佳答案
传递用户through the event。触发事件时,您应该有权访问Auth
门面。
事件:
class SomeEvent
{
// Public so that it will be accessible from the listener
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
}
听众:
class SomeListener
{
public function handle(SomeEvent $event)
{
// Get the public property `$user` from the event
$authUser = $event->user;
}
}
触发:
event(new SomeEvent(auth()->user()));
关于php - 如何在事件监听器中获取经过身份验证的用户-Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428159/