Search code examples
laravel-5laravel-facade

Laravel5, I like to place Event::listen() in a separate file, but the Event facade was unknown


I would like to have a set of Event::listen()s in a separate file, this is what I have done so far.

bootstrap/autoload.php

...
require __DIR__.'/../bootstrap/listeners.php';

bootstrap/listeners.php

<?php

use \Event;

Event::listen('illuminate.query', function($query) {
    dd($query);
});

The error is class Event not found

What is the right way for my requirement? I'm not sure creating a new .php file and require it from bootstrap/autoload.php is the right way for this requirement or not.

Please help. Thanks


Solution

  • In your case the Facades are not available in the bootstap, because the IOC Container of Laravel has not been create at this moment and so the Facades arent initialized yet.

    In L5 the EventListeners have to be registered in the boot of the EventServiceProvider (app/providers).

    http://laravel.com/docs/5.0/events