How do I catch exceptions / missing pages in Laravel 5?

前端 未结 8 1513
小鲜肉
小鲜肉 2020-11-27 13:11

In Laravel 5, App::missing and App::error is not available, so how do your catch exceptions and missing pages now?

I could not find any inf

相关标签:
8条回答
  • 2020-11-27 13:42

    With commit from today (9db97c3), all you need to do is add a 404.blade.php file in the /resources/views/errors/ folder and it will find and display it automatically.

    0 讨论(0)
  • 2020-11-27 13:47

    By Adding following code

    protected $dontReport = [
                'Symfony\Component\HttpKernel\Exception\HttpException'
        ];
    

    and

    public function render($request, Exception $e)
        {
        return redirect('/');
                //return parent::render($request, $e);
        }
    

    will work properly for me

    0 讨论(0)
  • 2020-11-27 13:52

    In Laravel 5 you can catch exceptions by editing the render method in app/Exceptions/Handler.php.

    If you want to catch a missing page (also known as NotFoundException) you would want to check if the exception, $e, is an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException.

    public function render($request, Exception $e) {
        if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
            return response(view('error.404'), 404);
    
        return parent::render($request, $e);
    }
    

    With the code above, we check if $e is an instanceof of Symfony\Component\HttpKernel\Exception\NotFoundHttpException and if it is we send a response with the view file error.404 as content with the HTTP status code 404.

    This can be used to ANY exception. So if your app is sending out an exception of App\Exceptions\MyOwnException, you check for that instance instead.

    public function render($request, Exception $e) {
        if ($e instanceof \App\Exceptions\MyOwnException)
            return ''; // Do something if this exception is thrown here.
    
        if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
            return response(view('error.404'), 404);
    
        return parent::render($request, $e);
    }
    
    0 讨论(0)
  • 2020-11-27 13:55

    One way you can handle it but it's not the best is to change over to a redirect.

    <?php namespace App\Exceptions;
    
    use Exception;
    use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
    
    class Handler extends ExceptionHandler {
    
            /**
             * A list of the exception types that should not be reported.
             *
             * @var array
             */
            protected $dontReport = [
                    'Symfony\Component\HttpKernel\Exception\HttpException'
            ];
    
            /**
             * Report or log an exception.
             *
             * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
             *
             * @param  \Exception  $e
             * @return void
             */
            public function report(Exception $e)
            {
                    return parent::report($e);
            }
    
            /**
             * Render an exception into an HTTP response.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Exception  $e
             * @return \Illuminate\Http\Response
             */
            public function render($request, Exception $e)
            {
            return redirect('/');
                    //return parent::render($request, $e);
            }
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:56

    Since commits 30681dc and 9acf685 the missing() method had been moved to the Illuminate\Exception\Handler class.

    So, for some time, you could do this:

    app('exception')->missing(function($exception)
    {
        return Response::view('errors.missing', [], 404);
    });
    


    ... But then recently, a refactoring has been done in 62ae860.

    Now, what you can do is adding the following to app/Http/Kernel.php:

    // add this...
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Response;
    
    class Kernel extends HttpKernel {
    
        (...)
    
        public function handle($request)
        {
            try
            {
                return parent::handle($request);
            }
    
            // ... and this:
            catch (NotFoundHttpException $e)
            {
                return Response::view('errors.missing', [], 404);
            }
    
            catch (Exception $e)
            {
                throw $e;
            }
        }
    


    Finally, please keep in mind Laravel is still under heavy development, and changes may occur again.

    0 讨论(0)
  • 2020-11-27 13:58

    In case you want to keep the handler in your web routes file, after your existing routes:

    Route::any( '{all}', function ( $missingRoute) {
        // $missingRoute
    } )->where('all', '(.*)');
    
    0 讨论(0)
提交回复
热议问题