Laravel 5 - 事件处理程序和侦听器之间的混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30336337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:35:24  来源:igfitidea点击:

Laravel 5 - Confusion between Event Handlers and Listeners

laravelevent-handlinglaravel-5event-listener

提问by user391986

I'm a bit confused about the different between Eventsand Listeners.

我对EventsListeners之间的不同感到有些困惑。

I understood how you can create your events under Eventsthen register them and implement the Handlers in Handlers\Events. So here I have events and the handling of events.

我了解如何在下面创建事件,Events然后注册它们并在Handlers\Events. 所以在这里我有事件和事件的处理。

They work after I define them in Providers\EventServiceProvider.php

他们在我定义它们之后工作 Providers\EventServiceProvider.php

protected $listen = [
    UserHasSignedUp::class => [
        SendWelcomeEmail::class,
        SendAdminEmail::class
    ]
];

So what are Listeners?

那么什么是监听器

To me they seem exactly the same thing as Event Handlers?

对我来说,它们似乎与事件处理程序完全相同?

回答by Ezequiel Moreno

In your example UserHasSignedUpis an Event. SendWelcomeEmailand SendAdminEmailare two listeners "waiting" for the event UserHasSignedUp to be fired and they should implement the required business logic at handlemethod of each one.

在您的示例中UserHasSignedUp是一个Event. SendWelcomeEmail并且SendAdminEmail是两个“等待”事件 UserHasSignedUp 被触发的侦听器,它们应该在handle每个方法中实现所需的业务逻辑。

Super simple example:

超级简单的例子:

Somewhere in UserController

UserController 中的某处

Event::fire(new UserHasSignedUp($user)); //UserHasSignedUp is the event being fired

SendWelcomeEmail class

SendWelcomeEmail 类

class SendWelcomeEmail //this is the listener class
{
    public function handle(UserHasSignedUp $event) //this is the "handler method"
    {
        //send an email
    }   
}

As you can see, each event can have multiple listeners, but a listener can't listen to more than a single event. If you want a class listening to many events, you should take a look to Event Subscribers

如您所见,每个事件可以有多个侦听器,但一个侦听器不能侦听多个事件。如果你想要一个听很多事件的类,你应该看看事件订阅者

Hope it helps.

希望能帮助到你。

回答by Nikhil Bhatia

The only difference between them seems to be is, handler:eventis from Laravel 5.0's folder structure, and make:listeneris the new & currentfolder structure. Functionally, they are the same! - Upgrade Guide to Laravel 5.1

它们之间的唯一区别似乎是,handler:event来自 Laravel 5.0 的文件夹结构,并且make:listener新的和当前的文件夹结构。在功能上,它们是相同的!- Laravel 5.1 升级指南

Commands & Handlers

The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.

Likewise, the app/Handlers directory has been renamed to app/Listenersand now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.

By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.

命令和处理程序

app/Commands 目录已重命名为 app/Jobs。但是,您不需要将所有命令移动到新位置,您可以继续使用 make:command 和 handler:command Artisan 命令来生成您的类。

同样,app/Handlers 目录已重命名为 app/Listeners,现在仅包含事件侦听器。但是,您不需要移动或重命名现有的命令和事件处理程序,您可以继续使用 handler:event 命令来生成事件处理程序。

通过为 Laravel 5.0 文件夹结构提供向后兼容性,您可以将您的应用程序升级到 Laravel 5.1,并在您或您的团队方便时将您的事件和命令缓慢升级到新位置。

It's just the backward compatibility provided in Laravel 5.1. In other words, earlier, Jobs/Commands/Listeners were not self-handling, now they are.

这只是 Laravel 5.1 提供的向后兼容性。换句话说,早些时候,Jobs/Commands/Listeners不是 self-handling,现在它们是.

Note that, handler:eventis not available after Laravel 5.1.

需要注意的是handler:event不可用Laravel 5.1之后

回答by JonTroncoso

There's not too much information on this out there, so this might just be speculation. I took a look at this videoand saw that you can use handlers with commands. I think if you're using commands, that makes sense to have all your handlers in one spot. However if you're not, then having a App\Handlers\Events\Whatevermight not be as desirable as App\Listeners\Whatever.

没有太多关于这方面的信息,所以这可能只是猜测。我看了这个视频,发现你可以使用带有命令的处理程序。我认为如果您正在使用命令,那么将所有处理程序集中在一个位置是有意义的。但是,如果您不是,那么拥有App\Handlers\Events\Whatever可能不如App\Listeners\Whatever.

回答by Michael Chemani

Listeners vs. Handlers :

侦听器与处理程序:

A listener listenfor a specific event to be fired. xxxxCreatedListener will only listen for xxxx

listen要触发的特定事件的侦听器。xxxxCreatedListener 只会监听 xxxx

A handler can handle multiple events to be fired. For exemple, let's say you use performing CRUD operations, your handler could wait for the xxxxCreatedEvent, xxxxDeletedEvent, xxxxUpdatedEvent.

一个处理程序可以处理多个要触发的事件。例如,假设您使用执行 CRUD 操作,您的处理程序可以等待 xxxxCreatedEvent、xxxxDeletedEvent、xxxxUpdatedEvent。