3 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
添加刀片頁(yè)面resources/views/errors/503.blade.php
您可以使用 Artisan 命令發(fā)布 Laravel 的錯(cuò)誤頁(yè)面模板vendor:publish
。模板發(fā)布后,您可以根據(jù)自己的喜好對(duì)其進(jìn)行自定義:
php?artisan?vendor:publish?--tag=laravel-errors
此命令將在目錄中創(chuàng)建所有自定義錯(cuò)誤頁(yè)面resources/views/errors/
。您可以根據(jù)需要進(jìn)行定制。

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
只需去掉 if 語(yǔ)句即可:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $exception)
{
return response()->view('errors.site_down', [], 503);
}
如果您試圖聲稱(chēng)該網(wǎng)站已關(guān)閉以進(jìn)行維護(hù),您可能還需要返回 503。
在批評(píng)這種方法時(shí),我認(rèn)為聲稱(chēng)網(wǎng)站正在維護(hù)您的錯(cuò)誤對(duì)您的用戶來(lái)說(shuō)是不誠(chéng)實(shí)和透明的,從長(zhǎng)遠(yuǎn)來(lái)看這不會(huì)得到回報(bào)。

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
對(duì)于自定義異常,首先您必須創(chuàng)建一個(gè)自定義異常文件,最好在異常文件夾中App\Exceptions\CustomException.php
<?php
namespace App\Exceptions;
use Exception;
class CustomException extends Exception
{
//
}
然后在你的異常處理程序文件中App\Exceptions\Handler.php
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\CustomException as CustomException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $exception)
{
// Thrown when a custom exception occurs.
if ($exception instanceof CustomException) {
return response()->view('error.page.path', [], 500);
}
// Thrown when an exception occurs.
if ($exception instanceof Exception) {
response()->view('errors.page.path', [], 500);
}
return parent::render($request, $exception);
}
}
請(qǐng)記住use App\Exceptions\CustomException;在需要拋出自定義異常的地方自定義異常文件,如下所示:
use App\Exceptions\CustomException;
function test(){
throw new CustomException('This is an error');
}
- 3 回答
- 0 關(guān)注
- 180 瀏覽
添加回答
舉報(bào)