I try to register events with a service provider, but continue to receive a reflection class error. I searched and could not find a solution, perhaps this is a problem with the kernel.
<?php namespace App;
use Illuminate\Support\ServiceProvider;
use App\Events\UserEventHandler;
class AppServiceProvider extends ServiceProvider {
public function register() {
$this->app->events->subscribe(new UserEventHandler);
}
}
<?php namespace App\Events;
class UserEventHandler {
public function onUserLogin($event)
{
echo 'subscriber logged in'; exit;
}
public function subscribe($events)
{
$events->listen('auth.login', 'UserEventHandler@onUserLogin');
}
}
$this->app->events->subscribe(new UserEventHandler);works fine because I can reach the subscription method, however it $events->listen('auth.login', 'UserEventHandler@onUserLogin');throws an exceptionClass UserEventHandler does not exist
I tried the same code on global.php and it works fine, so the problem lies with ServiceProvider, I tried both register () and boot () methods and got the same error.
[UPDATED]
I found that you need to specifically specify the full namespace when listening to the event.
public function subscribe($events)
{
$events->listen('auth.login', 'App\Events\UserEventHandler@onUserLogin');
}
source
share