PHP框架之Laravel5——安装Whoops错误处理

第一步:首先安装whoops:composer require filp/whoops:~1.0.

第二步:打开 app/Exceptions/Handler.php, 在render()方法中添加一个Whoops样式的处理情况,像下面这样

/**
     * 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)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }

        if (config('app.debug'))
        {
            return $this->renderExceptionWithWhoops($e);
        }

        return parent::render($request, $e);
    }

    /**
     * Render an exception using Whoops.
     * 
     * @param  \Exception $e
     * @return \Illuminate\Http\Response
     */
    protected function renderExceptionWithWhoops(Exception $e)
    {
        $whoops = new \Whoops\Run;
        $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());

        return new \Illuminate\Http\Response(
            $whoops->handleException($e),
            $e->getStatusCode(),
            $e->getHeaders()
        );
    }

原文地址:https://mattstauffer.com/blog/bringing-whoops-back-to-laravel-5/

猜你喜欢

转载自blog.csdn.net/mortal5/article/details/81362829