laravel 我在哪里放置事件侦听器和处理程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15933255/
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
Where do I put event listeners and handlers?
提问by Strernd
I am wondering where to put the Laravel Event Listeners and Handlers. Somebody told me that I can put them anywhere. This is what I have tried so far.
我想知道将 Laravel 事件侦听器和处理程序放在哪里。有人告诉我,我可以把它们放在任何地方。这是我迄今为止所尝试的。
# listeners/log.php
<?php
Event::listen('log.create', 'LogHandler@create');
# handlers/LogHandler.php
<?php
class LogHandler {
public function create(){
$character = new Character;
$character->name = "test";
$character->save();
}
}
# controllers/MainController.php
public function test(){
Event::fire('log.create');
return "fired";
}
# start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries',
app_path().'/listeners',
app_path().'/handlers',
));
回答by Phill Sparks
I'm going to assume that you're asking this because they're not working, rather than for confirmation of something that you've got working.
我将假设您问这个是因为它们不起作用,而不是确认您已经开始工作。
Whilst it is correct that you can put event listeners anywhere, you need to make sure they'll actually get included - Laravel doesn't search through your source code looking for them.
虽然您可以将事件侦听器放在任何地方是正确的,但您需要确保它们确实被包含在内 - Laravel 不会在您的源代码中搜索它们。
My favourite place to include such files is in start/global.php
. If you look at the bottom of the file you can see where the filters are included, you can do the same to include your listeners. It would be cleanest to keep them all in one listeners file, like all of your routes are in one routes file...
我最喜欢包含此类文件的地方是start/global.php
. 如果您查看文件的底部,您可以看到过滤器所在的位置,您也可以这样做以包含您的侦听器。将它们全部保存在一个侦听器文件中将是最干净的,就像您的所有路由都在一个路由文件中一样......
# start/global.php
require app_path().'/filters.php';
回答by Jeff Lambert
My personal opinionis that it's bad practice in general to lump event listeners in a single place. Sure, today you only need 2 or 3, but scope can be added to any project at any time, possible adding a lot more.
我个人的观点是,将事件侦听器集中在一个地方通常是不好的做法。当然,今天您只需要 2 或 3 个,但范围可以随时添加到任何项目中,可能会添加更多。
Instead, I generally create a directory underneath the app
directory (e.g. app/CompanyName
) and put all of my application specific code in there. To tell Laravel how to find your files, you can then update your composer.json llike this:
相反,我通常在该目录下创建一个目录app
(例如app/CompanyName
)并将我的所有应用程序特定代码放在那里。要告诉 Laravel 如何找到你的文件,你可以像这样更新你的 composer.json:
"autoload": {
"classmap": [
// ...
],
"psr-4": {
"CompanyName\" : "app/"
},
}
After that, be sure to run composer dump-autoload
.
之后,请务必运行composer dump-autoload
.
Now, you can create namespace directories inside of your custom application directory, like app/CompanyName/Events/
, and be able to segregate out your event listeners into groups that make sense, and put them inside of a service provider, for example:
现在,您可以在自定义应用程序目录中创建命名空间目录,例如app/CompanyName/Events/
,并且能够将事件侦听器分成有意义的组,并将它们放在服务提供程序中,例如:
<?php namespace CompanyName/Events;
// File: app/CompanyName/Events/LogEventsProvider.php
use Illuminate\Support\ServiceProvider;
class LogEventsProvider extends ServiceProvider
{
public function register()
{
Event::listen('log.create', 'CompanyName/Events/LogEventsProvider@create');
}
public function create()
{
// ...
}
}
Now you can add this service provider to your app/config/app.php
and be good to go, and have all of your related event listeners in a single file, and ALL of your event listeners in a single directory, yet separate so that if something goes wrong with one of them you don't have to search through ALL of them to find where the error is happening.
现在,您可以将此服务提供者添加到您的app/config/app.php
并且一切顺利,并将所有相关的事件侦听器放在一个文件中,并将所有事件侦听器放在一个目录中,但要分开,以便如果其中一个出现问题您不必搜索所有这些来查找发生错误的位置。
NB: I did not come up with this as a practice, but found it somewhere along the way. I however cannot remember where it was.
NB:我没有把它作为一种练习,而是在途中的某个地方发现的。然而,我不记得它在哪里。