只为生产设计 500 错误页面
Design 500 error page only for production
我正在寻找一种仅针对 HTTP(非 ajax/fetch)响应显示自定义 500 错误页面的现代 (Laravel 5.4) 方式。我阅读了一些主题,但每个回复看起来都像个把戏或已过时。 \App\Exceptions\Handler
里面应该有修改的地方,但是没找到"right way".
有没有一种简单的方法可以在 Laravel 5.4 中显示有关致命错误(未捕获,返回 500)的特定页面?
换句话说,当我的一个控制器出现语法错误时,它会显示 "Whoops something went wrong" 以及一些 HTML 和 500 错误代码。我想显示其他内容,使用与默认行为相同的规则(理想情况下仅适用于 HTML 浏览器,不适用于 ajax/fetch,等等)。
编辑:仅在生产环境中。
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php
. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. The HttpException
instance raised by the abort function will be passed to the view as an $exception variable.
来自本帖的选择"best answer":https://laracasts.com/discuss/channels/general-discussion/custom-error-page-er500
您可以修改 \App\Exceptions\Handler::render()
:
public function render($request, Exception $exception)
{
if (config('app.debug') && !$this->isHttpException($exception)) {
$exception = new \Symfony\Component\HttpKernel\Exception\HttpException(500);
}
return parent::render($request, $exception);
}
Your exception will be reported in the logs as usually, but woops page will be replaced by your 500.blade.php view.
有时您必须捕获特定的异常才能呈现错误视图。在 Laravel 5.4 中,您可以通过编辑 App\Exceptions\Handler class
中的 report() 方法来完成此操作
public function report(Exception $exception)
{
if ($exception instanceof CustomException) {
// here you can log the error and return the view, redirect, etc...
}
return parent::report($exception);
}
我正在寻找一种仅针对 HTTP(非 ajax/fetch)响应显示自定义 500 错误页面的现代 (Laravel 5.4) 方式。我阅读了一些主题,但每个回复看起来都像个把戏或已过时。 \App\Exceptions\Handler
里面应该有修改的地方,但是没找到"right way".
有没有一种简单的方法可以在 Laravel 5.4 中显示有关致命错误(未捕获,返回 500)的特定页面?
换句话说,当我的一个控制器出现语法错误时,它会显示 "Whoops something went wrong" 以及一些 HTML 和 500 错误代码。我想显示其他内容,使用与默认行为相同的规则(理想情况下仅适用于 HTML 浏览器,不适用于 ajax/fetch,等等)。
编辑:仅在生产环境中。
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a
resources/views/errors/404.blade.php
. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. TheHttpException
instance raised by the abort function will be passed to the view as an $exception variable.
来自本帖的选择"best answer":https://laracasts.com/discuss/channels/general-discussion/custom-error-page-er500
您可以修改 \App\Exceptions\Handler::render()
:
public function render($request, Exception $exception)
{
if (config('app.debug') && !$this->isHttpException($exception)) {
$exception = new \Symfony\Component\HttpKernel\Exception\HttpException(500);
}
return parent::render($request, $exception);
}
Your exception will be reported in the logs as usually, but woops page will be replaced by your 500.blade.php view.
有时您必须捕获特定的异常才能呈现错误视图。在 Laravel 5.4 中,您可以通过编辑 App\Exceptions\Handler class
中的 report() 方法来完成此操作public function report(Exception $exception)
{
if ($exception instanceof CustomException) {
// here you can log the error and return the view, redirect, etc...
}
return parent::report($exception);
}