Where do I put event listeners and handlers?

events, laravel, laravel-4, php

Solution

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.

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
require app_path().'/filters.php';

Problem

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. ``` # 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', )); ```

Original source