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\OrderEventListener@onOrderWasCreated'
        );
    }

    /**
     * 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));

然而,当我创建新订单时,我的日志文件中看不到任何内容。我错过了什么?

4

1 回答 1

4

可能你还没有添加OrderEventListenersubscribe属性中EventServiceProvider,它应该是这样的:

protected $subscribe = [
   'App\Listeners\OrderEventListener',
];

如果您添加了,请运行php artisan clear-compiled以确保您的应用程序使用当前版本的类。

于 2015-12-28T10:35:03.867 回答