简体   繁体   English

在模型构造函数中注入Dispatcher时,Laravel事件未触发

[英]Laravel events not firing when injecting Dispatcher in model constructor

I am trying to inject the Laravel event dispatcher in the constructor of my model from an artisan command but I'm unable to fire subscribed events from this dependency. 我试图从工匠命令中将Laravel事件分派器注入模型的构造函数中,但无法从此依赖项中触发订阅的事件。

It looks like Laravel injects a new instance of Illuminate\\Events\\Dispatcher instead of re-using the Application dispatcher. 看起来Laravel注入了Illuminate \\ Events \\ Dispatcher的新实例,而不是重新使用Application调度程序。 I have followed a Laracast video from Jeff Way for the implementation. 我按照Jeff Way的Laracast视频进行了实施。

I have added in app/start/global.php : 我在app / start / global.php中添加了:

require app_path() . 需要app_path()。 '/events.php'; '/events.php';

This references a file called events.php containing : 这引用了一个名为events.php的文件,其中包含:

Event::subscribe('MyApp\\event\\Handlers\\MyEventHandler'); Event :: subscribe('MyApp \\ event \\ Handlers \\ MyEventHandler');

The events.php file has a subscribe method with the 3 corresponding event methods as below : events.php文件具有一个subscription方法,具有以下3个相应的事件方法:

public function subscribe($events)
{
        $events->listen('event.start', 'MyApp\event\Handlers\MyEventHandler@onStart');
        $events->listen('event.error', 'MyApp\event\Handlers\MyEventHandler@onError');
        $events->listen('event.complete', 'MyApp\event\Handlers\MyEventHandler@onComplete');
}

In my Artisan command, I'm creating a model using Laravel IoC with : 在Artisan命令中,我正在使用Laravel IoC通过以下方式创建模型:

$model = \App::make('MyApp\models\MyModel');

And my model constructor is : 我的模型构造函数是:

use Illuminate\Events\Dispatcher;

class DataReplication {

    protected $events;

    public function __construct(Dispatcher $events)
    {
        $this->events = $events;
    }
}

At this point, a new instance of the dispatcher is injected and any listeners referenced in my event handler ('MyApp\\event\\Handlers\\MyEventHandler') is not in the listeners array. 此时,将注入新的分派器实例,并且在事件处理程序(“ MyApp \\ event \\ Handlers \\ MyEventHandler”)中引用的所有侦听器均不在侦听器数组中。 Thus when using $this->events->fire('event.start'); 因此,当使用$ this-> events-> fire('event.start');时, there's no handler to hook to. 没有处理程序可以钩住。

This seems a bit weird as using the Facade Event::fire('event.start'); 使用Facade Event :: fire('event.start');似乎有点不可思议。 from the same model would fire the 'onStart' method in my event handler. 来自同一模型的事件将在事件处理程序中触发“ onStart”方法。

Is there anything I'm missing out here ? 我有什么想念的吗? I'm under the impression that artisan commands sit in another scope beside the Application, and that only Laravel Facades manage to have a binding with the global application scope. 我的印象是,工匠命令位于应用程序旁边的另一个范围中,并且只有Laravel Facades才能与全局应用程序范围绑定。

Thanks for your help. 谢谢你的帮助。

使用\\Illuminate\\Contracts\\Events\\Dispatcher代替Illuminate\\Events\\Dispatcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM