【问题标题】:overwrite Laravel exception handler in Laravel package development在 Laravel 包开发中覆盖 Laravel 异常处理程序
【发布时间】:2021-09-17 16:43:43
【问题描述】:

我正在编写一个 Laravel 包,并且有一些路由返回 JSON 响应 我想在包中有自己的异常处理程序,而不是 Laravel 处理程序 但我无法覆盖它。有一个关于它的discussion,但它不再起作用了

我在我的包服务提供者和控制器构造方法中编写了这个单例,但它不起作用

我的服务提供商:

namespace Rabsana\Trade\Providers;
use Illuminate\Support\ServiceProvider;

class TradeServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $this->app->singleton(
            Illuminate\Contracts\Debug\ExceptionHandler::class,
            Rabsana\Trade\Exceptions\Handler::class
        );
    }


    public function register()
    {
        // 
    }

我的控制器:


use Illuminate\Routing\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class TestController extends Controller
{

    public function __construct()
    {
        \App::singleton(
            Illuminate\Contracts\Debug\ExceptionHandler::class,
            Rabsana\Trade\Exceptions\Handler::class
        );
    }
    public function index()
    {
        throw new ModelNotFoundException();
    }
}

我在包裹里的处理程序:

namespace Rabsana\Trade\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }


    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Throwable               $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $exception)
    {

        dd('here');
        return parent::render($request, $exception);
    }
}

抛出异常后,App\Exceptions\Handler.php 的渲染方法将被执行,但我希望 Rabsana\Trade\Exceptions\Handler.php 被执行

【问题讨论】:

    标签: php laravel exception package handler


    【解决方案1】:

    我不知道无需用户手动更改其App/Exceptions/Handler.php 文件或手动覆盖处理程序绑定的另一种方法来覆盖 Laravel 异常处理程序(除了您的示例中显示的方法)。

    您可以做的是,要求用户将一段代码添加到他们的App/Exceptions/Handler.php 文件中,这将触发您的包。

    Sentry 通过以下方式执行此操作:

    // App/Exceptions/Handler.php
    
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            if ($this->shouldReport($e) && app()->bound('sentry')) {
                app('sentry')->captureException($e);
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-26
      • 2015-09-03
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多