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

前端 未结 8 1584
小鲜肉
小鲜肉 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: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.

提交回复
热议问题