Laravel default route to 404 page

前端 未结 5 624
离开以前
离开以前 2021-02-08 08:51

I\'m using Laravel 4 framework and I\'ve defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?

相关标签:
5条回答
  • 2021-02-08 09:28

    The recommended method for handling errors can be found in the Laravel docs:

    http://laravel.com/docs/4.2/errors#handling-404-errors

    Use the App::missing() function in the start/global.php file in the following manner:

    App::missing(function($exception)
    {
        return Response::view('errors.missing', array(), 404);
    });
    
    0 讨论(0)
  • 2021-02-08 09:28

    according to the official documentation

    you can just add a file in: resources/views/errors/ called 404.blade.php with the information you want to display on a 404 error.

    0 讨论(0)
  • 2021-02-08 09:29

    Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception which you can handle in the app/start/global.php using the App::error() method like this:

    /**
     * 404 Errors
     */
    App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
    {
       // handle the exception and show view or redirect to a diff route
        return View::make('errors.404');
    });
    
    0 讨论(0)
  • 2021-02-08 09:37

    I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:

    App::missing(function($exception) {...});
    

    is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:

    public function handle($request) {
        try {
            return parent::handle($request);
        }
        catch (Exception $e) {
            echo \View::make('frontend_pages.page_404');
            exit;
            // throw $e;
        }
    }
    
    0 讨论(0)
  • 2021-02-08 09:43

    In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.

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