事件监听器未在Laravel 5.1中触发

时间:2016-03-08 11:06:08

标签: laravel laravel-5 laravel-5.1

我遇到一个问题,让我的事件监听器在Laravel 5.1中启动。

我正在解雇以下事件:

/**
 * Add new project.
 *
 * @param AddNewProjectRequest $request
 * @return Redirect
 */
public function add(AddNewProjectRequest $request) 
{
    // Event(s);
    Event::fire(new ProjectAdded($project, $request->only('file')));
}

我有以下事件设置:

<?php

namespace App\Events\Project;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ProjectAdded extends Event
{
    use SerializesModels;

    public $project;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($project)
    {
        $this->project = $project;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

这是我的EventServiceProvider文件:

<?php

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        \App\Events\ProjectAdded::class => [
            \App\Listeners\Project\ImportFileListener::class,
        ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        //
    }
}

这是我的ImportFileListener听众:

<?php

namespace Woodford\Listeners\Project;

use Woodford\Events\ProjectAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ImportFileListener 
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  ProjectAdded  $event
     * @return void
     */
    public function handle(ProjectAdded $event)
    {
        dd('listener');
    }
}

正如您在上面所看到的,我添加了dd('listener');来查看听众是否被解雇了 - 它没有!

我也尝试过php artisan optimizecomposer dump-autoload,但仍然没有运气!

有谁知道可能出现什么问题?

0 个答案:

没有答案