mouyong/php-support

v1.9.4
版本
2023-12-21
版本更新時(shí)間
1212
安裝
2
star
簡(jiǎn)介
php 基礎(chǔ)支持,詳細(xì)內(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
使用
控制器
- 通過(guò)
./webman make:controller
控制器生成后,繼承同目錄下的WebmanBaseController
基類。 - 編寫接口時(shí)可通過(guò)
$this->success($data = [], $err_code = 200, $messsage = 'success');
返回正確數(shù)據(jù)給接口。 - 編寫接口時(shí)可通過(guò)
$this->fail($messsage = '', $err_code = 400);
返回錯(cuò)誤信息給接口。 - 在
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); // 這里進(jìn)行調(diào)用,做了一些錯(cuò)誤捕捉
}
}
注意:頭請(qǐng)求未聲明此次請(qǐng)求需要返回 json 數(shù)據(jù)時(shí),$this->fail($message, $err_code)
的錯(cuò)誤碼需要符合 http status_code 響應(yīng)碼