1. Go to this page and download the library: Download carlin/laravel-data-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/ */
use Carlin\LaravelDataSwagger\Attributes\Property;
use Carlin\LaravelDataSwagger\Attributes\Schema;
use OpenApi\Attributes\Items;
#[Schema(dtoClass: __CLASS__, title: '翻译文本', description: '')]
class LanguageResource
{
#[Property(title: '翻译驱动', description: '翻译服务提供商', type: 'string', example: 'baidu', enumClass: Enums::class)]
public string $drive;
#[Property(title: '待翻译文本', type: 'string', example: '你好世界')]
public string $query;
#[Property(title: '目标语言', description: '目标语言代码', type: 'string', example: 'en')]
public string $to;
#[Property(title: '源语言', description: '默认auto自动检测', type: 'string', example: 'auto')]
public string $from;
}
#[Schema(dtoClass: __CLASS__, title: '创建请求', description: '')]
class LanguageCreateRequest
{
#[Property(title: '翻译驱动', description: '翻译服务提供商', type: 'string', example: 'baidu', enumClass: Enums::class)]
public string $drive;
#[Property(title: '待翻译文本', type: 'string', example: '你好世界')]
public string $query;
#[Property(title: '我是integer', description: '我是integer', type: 'integer', example: 1)]
public string $integer;
#[Property(title: '我是integer', description: '默认auto自动检测', type: 'boolean', example: 1)]
public bool $boolean;
#[Property(title: '我是number', description: '默认auto自动检测', type: 'number', example: 1)]
public float $number;
//请求是字符串数组
#[Property(title: 'array_string', type: 'array', items: new Items(type: 'string'))]
public ?array $array_string = [];
//请求是integer数组
#[Property(title: 'array_int', type: 'array', items: new Items(type: 'integer'))]
public ?array $array_int = [];
//请求是对象数组
#[Property(title: 'actions', type: 'array', items: new Items(ref: FooActions::class))]
public array $actions = [];
//请求对象
#[Property(ref: FooActions::class, title: 'action', type: 'object')]
public FooActions $action;
}
use Carlin\LaravelDataSwagger\Attributes\Additional\BaseResource;
use Carlin\LaravelDataSwagger\Attributes\Additional\SuccessResponse;
use Carlin\LaravelDataSwagger\Attributes\Post;
#[Tag(self::TAG, description: '语言管理')]
class LanguageController
{
public const TAG = '语言管理';
#[Post(controller: self::class, method: __FUNCTION__, summary: '创建语言', tags: [self::TAG])]
#[RequestBody(dtoClass: LanguageCreateRequest::class)]
#[SuccessResponse(content: new BaseResource(LanguageResource::class))]
public function create(UserRequest $request)
{
// ...
}
}
use Carlin\LaravelDataSwagger\Attributes\Additional\BaseResource;
use Carlin\LaravelDataSwagger\Attributes\Additional\PageResource;
use Carlin\LaravelDataSwagger\Attributes\Additional\SuccessResponse;
use Carlin\LaravelDataSwagger\Attributes\Post;
#[Tag(self::TAG, description: '语言管理')]
class LanguageController
{
public const TAG = '语言管理';
#[Post(controller: self::class, method: __FUNCTION__, summary: '获取语言列表', tags: [self::TAG])]
#[RequestBody(dtoClass: LanguageListRequest::class)]
#[SuccessResponse(content: new PageResource(LanguageResource::class))]
public function list(LanguageListRequest $request)
{
// ...
}
}
use Carlin\LaravelDataSwagger\Attributes\Additional\ArrayObjectResource;
use Carlin\LaravelDataSwagger\Attributes\Additional\SuccessResponse;
use Carlin\LaravelDataSwagger\Attributes\Post;
#[Tag(self::TAG, description: '语言管理')]
class LanguageController
{
public const TAG = '语言管理';
#[Post(controller: self::class, method: __FUNCTION__, summary: '获取语言列表', tags: [self::TAG])]
#[RequestBody(dtoClass: LanguageListRequest::class)]
#[SuccessResponse(content: new ArrayObjectResource(LanguageResource::class))]
public function list(UserRequest $request)
{
// ...
}
}
use Carlin\LaravelDataSwagger\Attributes\Additional\BaseResource;
#[SuccessResponse(content: new BaseResource(LanguageResource::class, documentation: 'api_v2'))]