问答 / 0 / 6 / 创建于 9年前
report()和render()方法是默认的,我这个环境还有如图这个unauthenticated()方法,请问这几个方法主要是干嘛用的呢?
你看下 app/Exceptions/Handler.php 继承的父类 /Illuminate/Foundation/Exceptions/Handler.php 这个文件你就懂了
app/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); }
@milkmeowo 第三个那个还是不太理解,而且你在这三个方法添加的代码和不添加有啥区别呢???
@Erchoc 不是我在这 3 个方法中添加代码,而是这里是父类也就是框架的内部实现,用来告诉你:
report()
render()
unauthenticated()
AuthenticationException
好吧,是我理解有误。那么 /Illuminate/Foundation/Exceptions是哪里呢?laravel文件夹下没有 Illuminate这个文件夹诶,这点我郁闷了挺久
@Erchoc vendor/laravel/framework/src/Illuminate/
vendor/laravel/framework/src/Illuminate/
@leo 懂了,谢谢
我要举报该,理由是:
你看下
app/Exceptions/Handler.php继承的父类 /Illuminate/Foundation/Exceptions/Handler.php 这个文件你就懂了@milkmeowo 第三个那个还是不太理解,而且你在这三个方法添加的代码和不添加有啥区别呢???
@Erchoc 不是我在这 3 个方法中添加代码,而是这里是父类也就是框架的内部实现,用来告诉你:
report()和render()方法默认的作用。都是继承于父类report()用来记录错误信息日志。render()渲染错误信息打印在浏览器中。unauthenticated()方法,是在父类render()方法中捕捉到AuthenticationException异常之后交给unauthenticated()方法去处理异常,而这个处理异常的逻辑就可以由你来决定,你只要在子类,也就是你的app/Exceptions/Handler.php中的unauthenticated()做出具体实现。好吧,是我理解有误。那么 /Illuminate/Foundation/Exceptions是哪里呢?laravel文件夹下没有 Illuminate这个文件夹诶,这点我郁闷了挺久
@Erchoc
vendor/laravel/framework/src/Illuminate/@leo 懂了,谢谢