laravel routing and 404 error

前端 未结 4 1450
予麋鹿
予麋鹿 2020-12-16 04:22

I want to specify that if anything entered in the url address other than existing routes (in routes.php, then show 404 page.

I know about this:

App:         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 04:56

    I would advice putting this into your app/start/global.php as that is where Laravel handles it by default (though filters.php will also work). I usually use something like this:

    /*
    |--------------------------------------------------------------------------
    | Application Error Handler
    |--------------------------------------------------------------------------
    |
    | Here you may handle any errors that occur in your application, including
    | logging them or displaying custom views for specific errors. You may
    | even register several error handlers to handle different types of
    | exceptions. If nothing is returned, the default error view is
    | shown, which includes a detailed stack trace during debug.
    |
    */
    
    App::error(function(Exception $exception, $code)
    {
        $pathInfo = Request::getPathInfo();
        $message = $exception->getMessage() ?: 'Exception';
        Log::error("$code - $message @ $pathInfo\r\n$exception");
    
        if (Config::get('app.debug')) {
            return;
        }
    
        switch ($code)
        {
            case 403:
                return Response::view('errors/403', array(), 403);
    
            case 500:
                return Response::view('errors/500', array(), 500);
    
            default:
                return Response::view('errors/404', array(), $code);
        }
    });
    

    Then just make an errors folder inside /views and place your error page content there. As Antonio mentioned you can pass data inside the array().

    I kindly borrowed this method from https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site

提交回复
热议问题