Laravel 4.1. * Event registration issue with the service provider

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.

// 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);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');
}
+4
source share
1 answer

, .

+5

Source: https://habr.com/ru/post/1533259/


All Articles