所以我試圖記錄一些簡單的事情,當我在工作的laravel 5應用程序中創建新訂單時。爲什麼我的事件偵聽器不在laravel 5中觸發?
所以我有一個OrderWasCreated
事件,這看起來像這樣:
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Order;
class OrderWasCreated extends Event
{
use SerializesModels;
public $order;
/**
* OrderWasCreated constructor.
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
然後,我有OrderEventListener
它看起來像這樣:
<?php
namespace App\Listeners;
use App\Events\OrderWasCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderEventListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function subscribe(Dispatcher $events)
{
$events->listen(
'App\Events\OrderWasCreated',
'App\Listeners\[email protected]'
);
}
/**
* Handle the event.
*
* @param OrderWasCreated $event
* @return void
*/
public function onOrderWasCreated(OrderWasCreated $event)
{
\Log::info('Order was created event fired');
}
}
在我OrderController
,我有這個在我的商店方法某處:
event(new OrderWasCreated($order));
然而,當我是crea對新訂單,我沒有看到我的日誌文件中的任何東西。我錯過了什麼?
你必須註冊你的事件與一個監聽器,如https://laravel.com/docs/5.1/events#registering-events-and-listeners你添加了一個監聽器與你的事件? –