我修改了Exceptions / Handler.php的呈现方法,以返回自己的响应,而不是在发生任何错误时呈现流明的错误页面。
这是我的渲染方法的更新。
/**
* 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 parent::render($request, $e);
return response()
->json([
'errors' => $e
]);
}
我希望在NotFoundHttpException发生时它返回我的响应,但是流明仍然显示其原始错误页面...
我知道我应该修改app/Exceptions/Handler.php
,但是由于它不符合我的预期,因此我改了vendor/laravel/lumen/app/Exceptions/Handler.php
。
这是更新的文件。
/**
* 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 response(['test'=> 'test']);
// return parent::render($request, $e);
}
它按预期工作(不显示错误页面,但返回json响应)。
在bootstrap / app.php中,似乎正确设置为调用App\Exceptions\Handler::class
,而不是laravel\lumen\app\Exceptions\Handler::class
。
/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
我已经尝试过重新安装lumen和docker环境。 而且我尝试在安装流明后立即修改Handler.php(在应用程序中没有其他修改),但是它不起作用。
有人知道我的更改没有得到体现吗?
答案 0 :(得分:0)
我为bootstrap/app.php
设置了错误的值。
我设置如下。
require_once __DIR__.'/../../vendor/autoload.php';
像下面这样修改了这一部分之后,流明开始能够调用Handler.php方法。
require_once __DIR__.'/../vendor/autoload.php';