如何在事件侦听器中获取经过身份验证的用户 - Laravel
How to get authenticated user in event listener - Laravel
我正在尝试确定在事件侦听器中获取经过身份验证的用户信息的正确方法。
我总是可以使用 \Auth:user()
,这似乎有效,但我认为如果我将侦听器排队,它就不会有效。
我是否需要将经过身份验证的用户添加到事件 class 作为每个事件的 属性,或者是否有任何其他方法可以做到这一点?
通过用户through the event。当事件被触发时,您应该可以访问 Auth
facade。
事件:
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()));
我正在尝试确定在事件侦听器中获取经过身份验证的用户信息的正确方法。
我总是可以使用 \Auth:user()
,这似乎有效,但我认为如果我将侦听器排队,它就不会有效。
我是否需要将经过身份验证的用户添加到事件 class 作为每个事件的 属性,或者是否有任何其他方法可以做到这一点?
通过用户through the event。当事件被触发时,您应该可以访问 Auth
facade。
事件:
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()));