Laravel 5覆盖异常处理程序 [英] Laravel 5 override exception handler

查看:119
本文介绍了Laravel 5覆盖异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在Laravel 5中重写应用程序异常处理程序类而不将其扩展到另一个类.

I'd like to know if it's possible to override the application exception handler class in Laravel 5 without extending it to another class.

也许更好的说法是,我希望它不是在异常中调用App\Exceptions\Handler,而是在我自己的处理程序中调用.

Maybe a better way of saying it is that I'd like it so that not App\Exceptions\Handler will be called on an exception but one of my own handlers.

谢谢.

推荐答案

如Digitlimit先前所述

As mentioned before by Digitlimit here Laravel ships with default ExceptionHandler which can be overwritten. There's two ways you can do so:

Laravel 5.8附带了在app/Exceptions/Handler.php中实现的默认ExceptionHandler.此类从Illuminate\Foundation\Exceptions\Handler扩展,而Illuminate\Foundation\Exceptions\HandlerIlluminate\Contracts\Debug\ExceptionHandler接口的实际Laravel实现.通过删除父类并自己实现接口,您可以执行所需的所有自定义异常处理.我在回答的末尾包含了Handler类的一个小示例实现.

Laravel 5.8 ships with a default ExceptionHandler implemented in app/Exceptions/Handler.php. This class extends from Illuminate\Foundation\Exceptions\Handler which is the actual Laravel implementation of the Illuminate\Contracts\Debug\ExceptionHandler interface. By removing the parent class and implementing the interface yourself you could do all the custom exception handling you'd like. I've included a small example implementation of the Handler class at the end of my answer.

实现自定义ExceptionHandler的另一种方法是覆盖可以在bootstrap/app.php中找到的默认配置.为了覆盖Larvel指定的处理程序,只需在默认默认值之后注册一个Illuminate\Contracts\Debug\ExceptionHandler::class抽象的单例即可.

Another way of implementing a custom ExceptionHandler is to overwrite the default configuration which can be found in bootstrap/app.php. In order to overwrite the handler specified by Larvel simply register a singleton for the Illuminate\Contracts\Debug\ExceptionHandler::class abstraction after the default one like so.

# Laravel default
$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

# My custom handler
$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\MyHandler::class
);

其结果是,如果抛出异常并且完全跳过了App\Exceptions\Handler,则会调用App\Exceptions\MyHandler.

The result of this is that App\Exceptions\MyHandler is called in case an exception is thrown and App\Exceptions\Handler is skipped altogether.

在这种情况下很有用,我提供了一个自定义ExceptionHandler的小例子,以全面了解它的可能性.

It case it's useful I've included a small example of a custom ExceptionHandler to give a global idea of it's possibilities.

namespace App\Exceptions;

use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Application;

class Handler implements ExceptionHandler
{
    public function report(Exception $e)
    {
        Log::debug($e->getMessage());
    }

    public function shouldReport(Exception $e)
    {
        return true;
    }

    public function render($request, Exception $e)
    {
        return view('error.page');
    }

    public function renderForConsole($output, Exception $e)
    {
        (new Application)->renderException($e, $output);
    }
}

这篇关于Laravel 5覆盖异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆