Laravel 4.1.* issue registering events in service provider
laravel, laravel-4
Solution
You need to declare the full path to the class if it is in a different directory to the service provider.
Problem
I am trying to register events with the service provider but I keep getting reflection class error. I have searched and could not find a solution, perhaps this is an issue with the core. ``` // AppServiceProvider.php <?php namespace App; use Illuminate\Support\ServiceProvider; use App\Events\UserEventHandler; class AppServiceProvider extends ServiceProvider { public function register() { // Events $this->app->events->subscribe(new UserEventHandler); } } // UserEventHandler.php <?php namespace App\Events; class UserEventHandler { /** * Handle user login events. */ public function onUserLogin($event) { echo 'subscriber logged in'; exit; } /** * Register the listeners for the subscriber. * * @param Illuminate\Events\Dispatcher $events * @return array */ public function subscribe($events) { $events->listen('auth.login', 'UserEventHandler@onUserLogin'); } } ``` `$this->app->events->subscribe(new UserEventHandler);` is firing fine, because I can reach the subscribe method, however `$events->listen('auth.login', 'UserEventHandler@onUserLogin');` is returning an exception `Class UserEventHandler does not exist` I have tried the same code on global.php and works fine, so the problem lies on the ServiceProvider, I have tried both methods register() and boot() and get the same error. [UPDATED] I have found that you need to specifically specify the full namespace when listening for the event. ``` public function subscribe($events) { $events->listen('auth.login', 'App\Events\UserEventHandler@onUserLogin'); } ```