事件监听器不触发
Laravel版本 5.8
PHP版本 7.3.1
win7
参考文档 事件系统《Laravel 5.8 中文文档》
注册
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\OrderShipped' => [
'App\Listeners\SendShipmentNotification',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
}
}
执行命令
php artisan event:generate
生成的事件文档
<?php
namespace App\Events;
use App\help\Helpers;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
class OrderShipped
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
Helpers::add_log('11');
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
Helpers::add_log('22');
return new PrivateChannel('channel-name');
}
}
生成的监听器文档
<?php
namespace App\Listeners;
use App\Events\OrderShipped;
use App\help\Helpers;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendShipmentNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
Helpers::add_log('33');
}
/**
* Handle the event.
*
* @param OrderShipped $event
* @return void
*/
public function handle(OrderShipped $event)
{
Helpers::add_log('44');
}
}
调用
event(new OrderShipped());
日志记录只有11出现
各位大侠还有什么原因会造成这样啊
补充
laravel-admin版本 1.7.6
事件和监听器测试继承队列, 执行命令 php artisan queue:work,还是没触发,
本地测试,还有什么命令没执行吗,或者还有什么文件要修改吗 ?
可能性1 命名空间写错 (因为类不存在 不会报错的 坑)
Laravel事件不执行排查
可能性2 有缓存
检查是否有这个缓存文件:
bootstrap/cache/events.php
如果有,手动删除或者
php artisan event:clear