讨论数量:
你看下 app/Exceptions/Handler.php
继承的父类 /Illuminate/Foundation/Exceptions/Handler.php 这个文件你就懂了
report 用来写入日志
/**
* Report or log an exception.
*
* @param \Exception $e
* @return void
*
* @throws \Exception
*/
public function report(Exception $e)
{
if ($this->shouldntReport($e)) {
return;
}
try {
$logger = $this->container->make(LoggerInterface::class);
} catch (Exception $ex) {
throw $e; // throw the original exception
}
$logger->error($e);
}
render 用来渲染各种异常的浏览器输出
/**
* Render an exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Exception $e)
{
$e = $this->prepareException($e);
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($e, $request);
}
return $this->prepareResponse($request, $e);
}
unauthenticated 可以看到 render 中捕捉到 AuthenticationException 异常,也就是没有认证时的渲染输出
elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
}
@Erchoc 不是我在这 3 个方法中添加代码,而是这里是父类也就是框架的内部实现,用来告诉你:
report()
和render()
方法默认的作用。都是继承于父类report()
用来记录错误信息日志。render()
渲染错误信息打印在浏览器中。
unauthenticated()
方法,是在父类render()
方法中捕捉到AuthenticationException
异常之后交给unauthenticated()
方法去处理异常,而这个处理异常的逻辑就可以由你来决定,你只要在子类,也就是你的app/Exceptions/Handler.php
中的unauthenticated()
做出具体实现。
推荐文章: