Laravel事件监听器

时间:2017-11-05 17:39:43

标签: php laravel

我在Laracast的帮助下创建了一个Event / Dispatch Stack。事件服务提供程序类如下所示:

<?php

namespace App\Events;

use Illuminate\Support\ServiceProvider;
use Illuminate\Log\Writer;

class EventingServiceProvider extends ServiceProvider {
    public function register() {
        $listeners =  $this->app['config']->get('medifaktor.listeners');

        foreach ($listeners as $listener) {
            $this->app['events']->listen('medifaktor.*', $listener);
        }
    }
}

这是一个监听器的示例:

<?php

namespace App\Listeners\Responder;

use App\Events\EventListener;
use App\Events\Responder\ResponderWasRegistered;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Events\Responder\ResponderStatusWasUpdated;
use Illuminate\Support\Facades\Log;

// Responder was registered
class ResponderRegistered extends EventListener {

    // Variables
    use DispatchesJobs;

    public function whenResponderWasRegistered(ResponderWasRegistered $event) {

    }
}

侦听器数组的输出:

array (
  0 => 'App\\Listeners\\Dispatch\\DispatchCreated',
  1 => 'App\\Listeners\\Incident\\IncidentCreated',
  2 => 'App\\Listeners\\MFUser\\MFUserRegistered',
  3 => 'App\\Listeners\\Responder\\ResponderDispatched',
  4 => 'App\\Listeners\\Responder\\ResponderStatusUpdated',
  5 => 'App\\Listeners\\Responder\\ResponderLocationUpdated',
  6 => 'App\\Listeners\\Responder\\ResponderRegistered',
)  



  $registerResponderCommand = new RegisterResponderCommand($bodyContent);
    $this->commandBus->execute($registerResponderCommand);

我现在正在使用Laravel 5.5,因为它似乎不再被$this->app['events']->listen识别。你如何正确调整?根据IDE中的调试消息,找不到此方法。这已经过时了吗?

0 个答案:

没有答案