【发布时间】: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