事件在 laravel 中看不到侦听器
The events don't see the listeners in laravel
我以前使用过事件和侦听器,但现在我尝试使用事件和侦听器,但事件看不到侦听器,如下面的代码所示
事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class teste
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct()
{
}
public function broadcastOn()
{
return [new PrivateChannel('channel-name')];
}
}
并且是事件的监听器
<?php
namespace App\Listeners;
use App\Events\teste;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class testl
{
public function __construct()
{
//
}
public function handle(teste $event)
{
return true;
}
}
和 eventProviderService
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
// Registered::class => [
// SendEmailVerificationNotification::class,
// ],
'App\Events\teste' => [
'App\Listeners\testl'
],
];
调用事件的代码是
Route::get("test100" , function(){
$Event = event(new \App\Events\teste());
dd($Event);
});
毕竟,当我使用函数 dd() 检查输出时没有输出它没什么它是空数组不包含 null 或任何东西
当我在侦听器中使用函数 dd() 来检查事件是否看到侦听器时,结果什么也没有
为了让 Event
和 Listener
发挥作用,您需要映射哪些 Listner
响应给定的 Event
。在 Laravel 中,这是在 EventServiceProvider
中处理的
下面的例子取自Laravel documentation:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\OrderShipped' => [
'App\Listeners\SendShipmentNotification',
],
];
任意数量的 Listener
可以对给定的 Event
做出反应
编辑:另外,请确保命名空间对您的应用程序来说是正确的
我已经通过从目录 bootstrap 和 运行 composer install
中删除现金文件解决了这个问题
我以前使用过事件和侦听器,但现在我尝试使用事件和侦听器,但事件看不到侦听器,如下面的代码所示
事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class teste
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct()
{
}
public function broadcastOn()
{
return [new PrivateChannel('channel-name')];
}
}
并且是事件的监听器
<?php
namespace App\Listeners;
use App\Events\teste;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class testl
{
public function __construct()
{
//
}
public function handle(teste $event)
{
return true;
}
}
和 eventProviderService
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
// Registered::class => [
// SendEmailVerificationNotification::class,
// ],
'App\Events\teste' => [
'App\Listeners\testl'
],
];
调用事件的代码是
Route::get("test100" , function(){
$Event = event(new \App\Events\teste());
dd($Event);
});
毕竟,当我使用函数 dd() 检查输出时没有输出它没什么它是空数组不包含 null 或任何东西 当我在侦听器中使用函数 dd() 来检查事件是否看到侦听器时,结果什么也没有
为了让 Event
和 Listener
发挥作用,您需要映射哪些 Listner
响应给定的 Event
。在 Laravel 中,这是在 EventServiceProvider
下面的例子取自Laravel documentation:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\OrderShipped' => [
'App\Listeners\SendShipmentNotification',
],
];
任意数量的 Listener
可以对给定的 Event
编辑:另外,请确保命名空间对您的应用程序来说是正确的
我已经通过从目录 bootstrap 和 运行 composer install
中删除现金文件解决了这个问题