如何在Laravel 8中注册自定义异常处理程序 [英] How to register custom exception handler in laravel 8
本文介绍了如何在Laravel 8中注册自定义异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Laravel 7中,此代码可以正常工作.在Laravel 8中也可以使用 renderable
方法.但是,我不确定在创建 CustomException
类之后如何在laravel 8中注册它.
In Laravel 7 this code works fine. Using renderable
method also works in laravel 8. But I'm not sure how to register it in laravel 8 after creating a CustomException
class.
public function render($request, Exception $exception)
{
if ($exception instanceof ValidationException) {
if ($request->expectsJson()) {
return response('Sorry, validation failed.', 422);
}
}
return parent::render($request, $exception);
}
推荐答案
这对我有用.
注册方法
public function register()
{
$this->renderable(function(Exception $e, $request) {
return $this->handleException($request, $e);
});
}
handleException的内容
The content of handleException
public function handleException($request, Exception $exception)
{
if($exception instanceof RouteNotFoundException) {
return response('The specified URL cannot be found.', 404);
}
}
希望您会发现它有用.
这篇关于如何在Laravel 8中注册自定义异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文