mouyong/php-support

v1.9.4
版本
2023-12-21
版本更新時間
1186
安裝
2
star
簡介
php 基礎(chǔ)支持,詳細內(nèi)容見 src/Utils
、src/Traits
目錄。
安裝
$ composer require zhenmu/support -vvv
基類創(chuàng)建控制器
./webman plugin:install zhen-mu/support
or
php ./vendor/zhenmu/support/src/scripts/install.php
使用
控制器
- 通過
./webman make:controller
控制器生成后,繼承同目錄下的WebmanBaseController
基類。 - 編寫接口時可通過
$this->success($data = [], $err_code = 200, $messsage = 'success');
返回正確數(shù)據(jù)給接口。 - 編寫接口時可通過
$this->fail($messsage = '', $err_code = 400);
返回錯誤信息給接口。 - 在
support/exception/Handler.php
的render
函數(shù)中,調(diào)用WebmanResponseTrait
的$this->renderableHandle($request, $exception);
示例如下:
<?php
namespace support\exception;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
use Webman\Exception\ExceptionHandler;
use ZhenMu\Support\Traits\WebmanResponseTrait;
/**
* Class Handler
* @package support\exception
*/
class Handler extends ExceptionHandler
{
use WebmanResponseTrait; // 這里需要引入 WebmanResponseTrait
public $dontReport = [
BusinessException::class,
];
public function report(Throwable $exception)
{
parent::report($exception);
}
public function render(Request $request, Throwable $exception): Response
{
return $this->renderableHandle($request, $exception); // 這里進行調(diào)用,做了一些錯誤捕捉
}
}
注意:頭請求未聲明此次請求需要返回 json 數(shù)據(jù)時,$this->fail($message, $err_code)
的錯誤碼需要符合 http status_code 響應(yīng)碼