Search code examples
phplaravellaravel-3

Laravel 3 - 404 event listener not being picked up from controllers


I'm trying to use the default event listener in Laravel for errors (i.e. 404).

I have the following code:

Event::listen('404', function()
{
    return Controller::call('errors@404');
});

This works fine (it calls the error controller's 404 action) if the URL doesn't exist (i.e. doesn't hit a controller). But when the path hits a controller, then it seems to ignore that event listener.

Do I need to do something to tell it to use that listener?


Solution

  • If you are using a catch-all route - like:

    Route::controller(Controller::detect())
    

    Then you are correct - the event does not seem to be captured by the Events class. It might be a bug - I'm not sure.

    However for 404 it is a really easy fix - just change your base_controller:

    public function __call($method, $parameters)
    {
        return Response::error('404');
    }
    

    to

    public function __call($method, $parameters)
    {
        Event::fire('404');
    }
    

    and it works as expected