如何禁用laravel 5.4默认错误处理程序

时间:2018-03-06 04:50:25

标签: php laravel laravel-5 laravel-5.4

我目前正在开展一个laravel项目。我需要将所有错误页面重定向到404页面未找到页面。

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版本。

4 个答案:

答案 0 :(得分:5)

在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);

答案 1 :(得分:2)

怎么样:

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

答案 2 :(得分:1)

resources/views/errors

中创建目录

在此目录中创建文件

404.blade.php表示404错误。

500.blade.php 400错误。

403.blade.php表示403错误。

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

希望这有帮助。

答案 3 :(得分:1)

检查以下代码是否在" handler.php"

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

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

您可以在处理程序中使用中止函数

相关问题