我的侦听器被调用了两次,但事件仅被触发一次。
我从Lumen 5.7示例中获取了代码,不知道我的错误在哪里。
bootstrap / app.php
$app->register(App\Providers\EventServiceProvider::class);
app / Providers / EventServiceProvider.php
protected $listen = [
'App\Events\NewAuthEvent' => [
'App\Listeners\SendNewAuthListener',
],
];
app / Events / NewAuthEvent.php
use Illuminate\Queue\SerializesModels;
use App\Data\Entity\User;
use App\Data\Entity\Authorization;
use Illuminate\Support\Facades\Log;
class NewAuthEvent
{
use SerializesModels;
public $user;
public $auth;
public function __construct(User $user, Authorization $auth)
{
Log::debug("once :)");
$this->user = $user;
$this->auth = $auth;
}
}
app / Listeners / SendNewAuthListener.php
use App\Events\NewAuthEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Data\Service\MessengerService;
use Illuminate\Support\Facades\Log;
class SendNewAuthListener
{
private $messenger;
public function __construct(MessengerService $messenger)
{
Log::debug("twice :(");
$this->messenger = $messenger;
}
public function handle(NewAuthEvent $event)
{
Log::debug("twice as well :(");
$this->messenger->new($event->user, $event->auth);
}
}
触发事件:
event(new NewAuthEvent($objUser, $objAuthorization));
我想念什么?还有其他要设置的参数或配置吗?
谢谢!
答案 0 :(得分:0)
我遇到了同样的问题。检查您的 EventsServiceProvider
。如果您使用的是返回 true 的 shouldDiscoverEvents
方法,删除它可能会解决您的问题。查看 andnacho here 的评论。