laravel routing and 404 error

前端 未结 4 1446
予麋鹿
予麋鹿 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:41

    This is my approach just for displaying the error 404 with a template layout. Just add the following code to /app/start/global.php file

    App::missing(function($exception)
    {
        $layout = \View::make('layouts.error');
        $layout->content = \View::make('views.errors.404');
        return Response::make($layout, 404);
    });
    
    0 讨论(0)
  • 2020-12-16 04:49

    You can add this to your filters.php file:

    App::missing(function($exception)
    {
        return Response::view('errors.missing', array(), 404);
    });
    

    And create the errors.missing view file to show them the error.

    Also take a look at the Errors & Logging docs

    EDIT

    If you need to pass data to that view, the second parameter is an array you can use:

    App::missing(function($exception)
    {
        return Response::view('errors.missing', array('url' => Request::url()), 404);
    });
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-16 05:00

    Just for guys using Laravel 5, there is an errors folder in in the views directory. You just need to create a 404.blade.php file there and it will render this view when there is no route specified for a url

    0 讨论(0)
提交回复
热议问题