我想知道将 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',
));