如何在Laravel 5中捕获异常/缺失页面?

时间:2014-10-29 12:57:03

标签: laravel laravel-5

在Laravel 5中,App::missingApp::error不可用,那么您现在如何捕获异常和丢失页面?

我在文档中找不到任何相关信息。

7 个答案:

答案 0 :(得分:93)

在Laravel 5中,您可以通过编辑render中的app/Exceptions/Handler.php方法来捕获例外。

如果您想要捕获缺失的页面(也称为NotFoundException),您需要检查异常$e是否是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);
}

根据上面的代码,我们会检查$e是否为Symfony\Component\HttpKernel\Exception\NotFoundHttpException error.404,如果是,我们会发送instanceof response文件{ {1}}作为view的内容。

这可用于任何例外情况。因此,如果您的应用发送App\Exceptions\MyOwnException例外,请检查该实例。

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);
}

答案 1 :(得分:15)

使用今天的提交(9db97c3),你需要做的就是在/ resources / views / errors /文件夹中添加一个404.blade.php文件,它会自动找到并显示它。

答案 2 :(得分:4)

自提交30681dc9acf685后,missing()方法已移至Illuminate\Exception\Handler类。

所以,有一段时间,你可以这样做:

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


......但最近,在62ae860进行了重构。

现在,您可以做的是将以下内容添加到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;
        }
    }


最后,请记住Laravel仍处于重大发展阶段,可能会再次发生变化。

答案 3 :(得分:2)

Angular HTML5模式可能会导致一堆丢失的页面(用户书签很少页面并尝试重新加载)。如果您无法覆盖服务器设置来处理它,您可能会考虑覆盖缺少页面行为。

您可以修改\ App \ Exceptions \ Handler渲染方法以使用200推送内容,而不是使用404代码推送404模板。

/**
 * 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)
{
    if ($this->isHttpException($e) && $e->getStatusCode() == 404)
    {
        return response()->view('angular', []);
    }
    return parent::render($request, $e);
}

答案 4 :(得分:2)

如果您希望将处理程序保留在Web路由文件中,请在现有路径之后:

Route::any( '{all}', function ( $missingRoute) {
    // $missingRoute
} )->where('all', '(.*)');

答案 5 :(得分:0)

添加以下代码

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

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

将适合我

答案 6 :(得分:-2)

您可以处理它的一种方法,但不是最好的方法是切换到重定向。

<?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);
        }

}
相关问题