Laravelの例外処理

Laravelの例外処理

タグ(スペースで区切っ):PHP

カスタム例外クラス

<?php
namespace App\Exceptions;

use Throwable;
use Exception;

class ApiException extends Exception
{

    public function __construct($code, \Throwable $previous = null)
    {
        parent::__construct(config('jsoncode.code')[(int) $code], $code, $previous);
    }

    /**
     * 报告异常
     *
     * @return void
     */
    public function report()
    {
        //
    }

    /**
     * 转换异常为 HTTP 响应
     *
     * @param  \Illuminate\Http\Request
     * @return \Illuminate\Http\Response
     */
    public function render($request)
    {
         return response()->json([
             'code' => $this->getCode(),
             'message' => $this->getMessage(),
        ]);
    }
}

renderメソッドのアプリ/例外/ハンドラクラスを変更します

getenv() 获取env配置信息

例外を投げます

throw new ApiException(500);

おすすめ

転載: www.cnblogs.com/yanweifeng/p/10955490.html