【问题标题】:how to disable laravel 5.4 default error handler如何禁用 laravel 5.4 默认错误处理程序
【发布时间】:2018-03-06 04:50:25
【问题描述】:

我目前正在做一个 laravel 项目。我需要将所有错误页面重定向到 404 page not found 页面。

public function render($request, Exception $exception)
        {
            if ($this->isHttpException($exception)) {
                switch ($exception->getStatusCode()) {

                        // not authorized
                        case '403':
                                return \Response::view('404',array(),403);
                                break;

                        // not found
                        case '404':
                                return \Response::view('404',array(),404);
                                break;

                        // internal error
                        case '500':
                                return \Response::view('404',array(),500);
                                break;

                        default:
                                return $this->renderHttpException($exception);
                                break;
                }
        } else {
                return parent::render($request, $exception);
        }



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

是否有将错误页面重定向到 404 页面?此代码中也未显示验证错误(发生验证错误时重定向到 404)。我使用的是 5.4 版本。

【问题讨论】:

  • 这个方法你试过了吗,return abort(404);
  • 感谢您的评论。已经检查过了。

标签: php laravel laravel-5 laravel-5.4


【解决方案1】:

它在 Laravel 5.5 上修改的 Laravel 5.4 的 Bug

https://github.com/laravel/framework/pull/18481

更改文件 vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php

if (! $this->isHttpException($e) && config('app.debug')) {
        return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
    }
    if (! $this->isHttpException($e)) {
       return \Response::view('404',array(),500);
    }
    return $this->toIlluminateResponse($this->renderHttpException($e), $e);

【讨论】:

  • 欢迎@AneeshAjithkumar
【解决方案2】:

怎么样:

if ($this->isHttpException($exception)) {
    abort(404);
}

【讨论】:

    【解决方案3】:

    resources/views/errors中创建目录

    在这个目录下创建文件

    404.blade.php 为 404 错误。

    500.blade.php 为 400 错误。

    403.blade.php 为 403 错误。

    这些视图将自动呈现。 对于中止应用程序,您可以使用abort(404)

    希望这会有所帮助。

    【讨论】:

    • 已经像你描述的那样在我的项目中拥有这些刀片......但没有在页面中显示验证错误。
    • 确保你的网络路由在web中间件中
    【解决方案4】:

    检查下面的代码是否在“handler.php”中

    "使用 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;"

    并检查错误资源文件。
    (https://laracasts.com/discuss/channels/general-discussion/how-do-i-create-a-custom-404-error-page)

    您可以在处理程序中使用中止功能

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 2016-07-17
      • 2016-06-20
      相关资源
      最近更新 更多