亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在維護視圖頁面下顯示 Laravel 應用程序中所有錯誤/異常的站點

在維護視圖頁面下顯示 Laravel 應用程序中所有錯誤/異常的站點

PHP
慕虎7371278 2023-07-08 17:41:03
我是 Laravel 的初學者。我遇到了一個 Laravel 應用程序。因為我需要處理所有類型的異常/錯誤。ViewExceptions、ErrorExceptions 等異常。我需要為所有這些系統異常、錯誤以及所有數據庫或編碼異常和錯誤顯示一個視圖頁面(正在維護的站點)。我檢查了Laravel 錯誤處理并在 google 上搜索了解決方案。但我搜索的越多,我對解決方案感到困惑。由于應用程序已經投入生產,我無法更改每個控制器來處理異常。我猜測,我只需要在 App/Exception/Handler 類中進行更改,但不確定它將如何工作。表單搜索我發現我必須像在 Handler 類中那樣進行更改:/**?* 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){? ? if ($exception instanceof CustomException) {? ? ? ? return response()->view('errors.site_down', [], 500);? ? }? ? return parent::render($request, $exception);}上面的代碼沒有顯示是否存在 ViewException。我觀察到,在 .env APP_DEBUG 中為 true,在 config/app 中為 false。有影響嗎?如何將所有異常或錯誤重定向到site_down頁面?還請指導我 laravel 中的異常和錯誤處理。我越來越困惑了。提前致謝。
查看完整描述

3 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

添加刀片頁面resources/views/errors/503.blade.php

您可以使用 Artisan 命令發布 Laravel 的錯誤頁面模板vendor:publish。模板發布后,您可以根據自己的喜好對其進行自定義:

php?artisan?vendor:publish?--tag=laravel-errors

此命令將在目錄中創建所有自定義錯誤頁面resources/views/errors/。您可以根據需要進行定制。

查看完整回答
反對 回復 2023-07-08
?
ITMISS

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。


在批評這種方法時,我認為聲稱網站正在維護您的錯誤對您的用戶來說是不誠實和透明的,從長遠來看這不會得到回報。


查看完整回答
反對 回復 2023-07-08
?
蠱毒傳說

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

}


查看完整回答
反對 回復 2023-07-08
  • 3 回答
  • 0 關注
  • 183 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號