[英]Laravel/Lumen event listener does not listen
I have a Lumen controller class that fires an event when a user is created. 我有一个Lumen控制器类,可以在创建用户时触发事件。 I am using an event dispatcher.
我正在使用事件调度程序。 The event gets fired as it should, but the listener does not handle the event.
该事件应该被触发,但是侦听器不处理该事件。 I am sure I have followed every step of the Lumen documentation.
我确信我已经遵循了Lumen文档的每一步。
// UserController.php
class UserController extends ApiController
{
protected $event = null;
public function __construct(Dispatcher $event)
{
$this->event = $event;
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->acceptContentType($request, 'json');
$this->input = $request->json()->all();
$this->withEncryptedParameters();
$this->validateParameterNames(array_keys($this->validationRules));
$this->validateParameterContent($this->validationRules);
$roles = $this->getRelationInput('roles');
$user = User::create($this->input);
$this->addRelation($user, $roles, Role::class, 'roles');
$this->event->fire(new UserCreated($user));
return $this->respondCreated($user->id);
}
}
So I basically want to store a user into the database and fire an event when that happens. 所以我基本上想要将用户存储到数据库中,并在发生这种情况时触发事件。
// UserCreated.php
class UserCreated extends Event
{
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
}
The event is fired correctly, so if I put an "echo" or a "var_dump" into the event's constructor, I can see that it works. 事件被正确触发,所以如果我将“echo”或“var_dump”放入事件的构造函数中,我可以看到它有效。 If I so the same for the listener, it does not react.
如果我对听众如此相同,它就没有反应。
// UserCreatedEmail.php
class UserCreatedEmail extends Listener
{
public function handle(UserCreated $event)
{
echo 'Hello?';
}
}
I have registered it in the EventServiceProvider. 我在EventServiceProvider中注册了它。
// EventServiceProvider.php
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
UserCreated::class => [
UserCreatedEmail::class
]
];
}
And uncommented it in the bootstrap area. 并在引导区域取消注释。
// bootstrap/app.php
$app->register(WISSIT\UserService\Providers\EventServiceProvider::class);
I have absolutely no idea why is doesn't work. 我完全不知道为什么不起作用。 I could use "$event->listen" but then it would also listen when I use testing.
我可以使用“$ event-> listen”但是当我使用测试时它也会听。 According to the Lumen documentation, it should also work without that.
根据Lumen文档,它应该没有它。
And yes, the namespaces are set correctly. 是的,命名空间设置正确。 And no, I do not want to use Facades.
不,我不想使用Facades。
In bootstrap/app.php under 'Register Service Providers' comment out the registration of the service provider. 在“注册服务提供商”下的bootstrap / app.php中注释掉服务提供商的注册。
// $app->register(App\Providers\EventServiceProvider::class);
into 成
$app->register(App\Providers\EventServiceProvider::class);
I think I already got this problem, so this is how I resolve this: 我想我已经遇到了这个问题,所以这就是我解决这个问题的方法:
In your EventServiceProvider
change the event class and listener class to a real path, dont use ::class
in EventServiceProvider
. 在
EventServiceProvider
将事件类和侦听器类更改为实际路径,不要在EventServiceProvider
使用::class
。 Ie: 即:
// EventServiceProvider.php
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'WISSIT\UserService\Events\UserCreated' => [
'WISSIT\UserService\Listeners\UserCreatedEmail'
]
];
}
Okay, so it appears like the event is only listened when using \\Illuminate\\Contracts\\Event\\Dispatcher
in the controller. 好的,所以看起来只有在控制器中使用
\\Illuminate\\Contracts\\Event\\Dispatcher
时才会收听\\Illuminate\\Contracts\\Event\\Dispatcher
。 I used \\Illuminate\\Events\\Dispatcher
. 我使用
\\Illuminate\\Events\\Dispatcher
。 I don't know why that is the case, but it worked. 我不知道为什么会这样,但它确实有效。
I had a similar issue when working with Lumen and event listeners. 在与Lumen和事件监听器一起工作时,我遇到了类似的问题。 The fired event never reached my custom listener and I was struggling for a day to figure out where the problem is.
被解雇的事件从未到达我的自定义听众,我正在努力争取一天找出问题所在。
At the end I figured out, that I had a wrong signature of handle
method on my listener. 最后我想通了,我的监听器上有一个错误的
handle
方法签名。 It was my mistake, but Dispatcher
did not notify me about that issue. 这是我的错,但
Dispatcher
没有通知我这个问题。 When I changed the method to accept given arguments, it just started working. 当我改变方法以接受给定的参数时,它才开始工作。
I think the problem lies in the Illuminate\\Events\\Dispatcher
in the fire
method. 我认为问题在于
fire
方法中的Illuminate\\Events\\Dispatcher
。 Function call_user_func_array
returns false if the signature of the method is wrong, but dispatcher just breaks out of the loop on error. 如果方法的签名错误,则函数
call_user_func_array
返回false,但调度程序在出错时突然退出循环。 And does not notify user about an issue. 并且不会通知用户有关问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.