简体   繁体   中英

Laravel 4 - Handling 404 Errors

I know that to handle 404 errors with laravel 4 is to write at app/start/global.php :

App::missing(function($exception)
{
    return Redirect::route('404_Error');
});

But actually I want to use this route:

Route::get('error', array(
    'as' => '404_Error',
    'uses' => 'ErrorController@get404Error'
));

But to stay at the same URL.

Example:

My URL right now is localhost:8000/users/blat (404 Error). I don't want redirect to error page. I want to see ErrorController@get404Error at this URL.

Thanks so much.

You may try something like this:

App::missing(function($exception)
{
    return App::make('ErrorController')->get404Error($exception);
});

Your ErrorController :

class ErrorController extends BaseController {
    //...
    public function get404Error($exception)
    {
        //...
        $data = ...;
        return View::make('error_view')->with('data', $data);
    }
}

If you use a different route it will redirect to that route, you probably will need to render your view right away, IMO it's the only way to stay where you are and not have your url changed:

App::missing(function($exception)
{
    return View::make('404_Error');
});

You can just create an instance of the ErrorController class and call its get404Error method. That way you will have the variables from your BaseController class too and the route will stay the same.

App::missing(function($exception)
{
    $errorController = new ErrorController();
    return $errorController->get404Error();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM