Laravel - 为 WEB 艺术家创造的 PHP 框架。

PHP THAT DOESN'T HURT. CODE HAPPY & ENJOY THE FRESH AIR.

Laravel logo

把 Whoops 样式错误处理器带回 Laravel 5

(这部分是关于Laravel5.0的一系列新特性的讲解,敬请关注后续更新!)

你也许仍然想念着Laravel 4 中那个“漂亮的”Whoops样式的错误处理器,下面告诉你如何在Laravel 5中使用它。

首先,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()
        );
    }

就是这么简单!

感谢Laracasts论坛中的这个帖子,是它让我想到要写出这么一篇博文。


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

We are clever, But we are clueless