Laravel5.3 关于 Handler 类默认方法的提问?

file

report()和render()方法是默认的,我这个环境还有如图这个unauthenticated()方法,请问这几个方法主要是干嘛用的呢?

这里是寻求创业团队的知识分子。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6

你看下 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);
        }
7年前 评论

@milkmeowo 第三个那个还是不太理解,而且你在这三个方法添加的代码和不添加有啥区别呢???

7年前 评论

@Erchoc 不是我在这 3 个方法中添加代码,而是这里是父类也就是框架的内部实现,用来告诉你:

  • report()render() 方法默认的作用。都是继承于父类
    • report() 用来记录错误信息日志。
    • render() 渲染错误信息打印在浏览器中。
  • unauthenticated() 方法,是在父类 render() 方法中捕捉到 AuthenticationException 异常之后交给 unauthenticated() 方法去处理异常,而这个处理异常的逻辑就可以由你来决定,你只要在子类,也就是你的 app/Exceptions/Handler.php 中的 unauthenticated() 做出具体实现。
7年前 评论

好吧,是我理解有误。那么 /Illuminate/Foundation/Exceptions是哪里呢?laravel文件夹下没有 Illuminate这个文件夹诶,这点我郁闷了挺久

7年前 评论
leo

@Erchoc vendor/laravel/framework/src/Illuminate/

7年前 评论

@leo 懂了,谢谢

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!