PHP code example of webman-tech / swagger
1. Go to this page and download the library: Download webman-tech/swagger library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webman-tech / swagger example snippets
namespace app\swagger;
use OpenApi\Attributes as OA;
/**
* @link https://swagger.io/specification/#info-object
*/
#[OA\Info(version: '1.0.0', title: 'My App')]
#[OA\Server(url: '/api', description: 'localhost')]
class OpenapiSpec
{
}
use OpenApi\Annotations as OA;
'openapi_doc' => [
'modify' => function(OA\OpenApi $openApi) {
$openApi->info->title = 'My App';
$openApi->info->version = '1.0.0';
$openApi->servers = [
new OA\Server(['url' => '/api', 'description' => 'localhost']),
];
}
]
use Webman\Route;
use WebmanTech\Swagger\Swagger;
Route::group('/api1', function () {
Swagger::create()->registerRoute([
'route_prefix' => '/openapi',
'openapi_doc' => [
'scan_path' => app_path() . '/controller/api1',
],
]);
Route::get('/test', [\app\controller\Test::class, 'index']);
});
Route::group('/api2', function () {
Swagger::create()->registerRoute([
'route_prefix' => '/my-doc',
'openapi_doc' => [
'scan_path' => app_path() . '/controller/api2',
],
]);
Route::get('/test', [\app\controller\Test::class, 'index']);
});
namespace app\form;
use WebmanTech\DTO\Attributes\ValidationRules;
use WebmanTech\DTO\BaseRequestDTO;
use WebmanTech\DTO\BaseResponseDTO;
class TestForm extends BaseRequestDTO {
/**
* 名称
* @example webman
*/
public string $name = '';
/**
* 年龄
* @example 5
*/
#[ValidationRules(max: 100)]
public int $age = 0;
/**
* 备注
*/
public string $remark = '';
public function doSomething(): TestFormResult {
return new TestFormResult(
name: 'abc',
)
}
}
class TestFormResult extends BaseResponseDTO {
public function __construct(
/**
* 名称
*/
public string $name,
) {
}
}
use app\form\TestForm;
use OpenApi\Attributes as OA;
use WebmanTech\Swagger\DTO\SchemaConstants;
class IndexController {
#[OA\Post(
path: '/xxx',
summary: '接口说明',
x: [
SchemaConstants::X_SCHEMA_REQUEST => TestForm::class . '@doSomething'
],
)]
public function md(Request $request) {
return TestForm::fromRequest($request)
->doSomething()
->toResponse();
}
#[OA\Get(
path: '/xxx',
summary: '接口说明',
x: [
SchemaConstants::X_SCHEMA_REQUEST => TestForm::class . '@doSomething'
],
)]
public function md(Request $request) {
return TestForm::fromRequest($request)
->doSomething()
->toResponse();
}
}