3 回答

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

TA貢獻1871條經驗 獲得超8個贊
只需去掉 if 語句即可:
/**
* 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);
}
如果您試圖聲稱該網站已關閉以進行維護,您可能還需要返回 503。
在批評這種方法時,我認為聲稱網站正在維護您的錯誤對您的用戶來說是不誠實和透明的,從長遠來看這不會得到回報。

TA貢獻1895條經驗 獲得超3個贊
對于自定義異常,首先您必須創建一個自定義異常文件,最好在異常文件夾中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);
}
}
請記住use App\Exceptions\CustomException;在需要拋出自定義異常的地方自定義異常文件,如下所示:
use App\Exceptions\CustomException;
function test(){
throw new CustomException('This is an error');
}
- 3 回答
- 0 關注
- 183 瀏覽
添加回答
舉報