1. Go to this page and download the library: Download tangwei/apidocs 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/ */
public function add(#[RequestBody] DemoBodyRequest $request){}
public function add(#[RequestQuery] DemoQuery $request){}
public function fromData(#[RequestFormData] DemoFormData $formData){}
#[ApiFormData(name: 'photo', format: 'binary')]
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}
#[ApiSecurity('Authorization')]
public function getUserInfo(DemoToken $header){}
use Hyperf\ApiDocs\Annotation\ApiResponse;
use Hyperf\DTO\Type\PhpType;
#[ApiResponse([PhpType::BOOL], 201)]
#[ApiResponse([PhpType::INT], 202)]
#[ApiResponse([PhpType::FLOAT], 203)]
#[ApiResponse([PhpType::ARRAY], 204)]
#[ApiResponse([PhpType::OBJECT], 205)]
#[ApiResponse([PhpType::STRING], 206)]
#[ApiResponse([Address::class], 207)]
#[ApiResponse([PhpType::INT], 208)]
#[ApiResponse([PhpType::BOOL])]
public function test(){}
use Hyperf\ApiDocs\Annotation\ApiVariable;
class Page
{
public int $total;
#[ApiVariable]
public array $content;
public function __construct(array $content, int $total = 0)
{
$this->content = $content;
$this->total = $total;
}
}
#[ApiOperation('分页')]
#[GetMapping(path: 'activityPage')]
#[ApiResponse(new Page([ActivityResponse::class]))]
public function activityPage(#[RequestQuery] PageQuery $pageQuery): Page
{
$activityPage = Activity::paginate($pageQuery->getSize());
$arr = [];
foreach ($activityPage as $activity) {
$arr[] = ActivityResponse::from($activity);
}
return new Page($arr, $activityPage->total());
}
#[Controller(prefix: '/demo')]
#[Api(tags: 'demo管理', position: 1)]
class DemoController extends AbstractController
{
#[ApiOperation(summary: '查询')]
#[PostMapping(path: 'index')]
public function index(#[RequestQuery] #[Valid] DemoQuery $request): Contact
{
$contact = new Contact();
$contact->name = $request->name;
var_dump($request);
return $contact;
}
#[PutMapping(path: 'add')]
#[ApiOperation(summary: '提交body数据和get参数')]
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query)
{
var_dump($query);
return json_encode($request, JSON_UNESCAPED_UNICODE);
}
#[PostMapping(path: 'fromData')]
#[ApiOperation(summary: '表单提交')]
#[ApiFormData(name: 'photo', type: 'file')]
public function fromData(#[RequestFormData] DemoFormData $formData): bool
{
$file = $this->request->file('photo');
var_dump($file);
var_dump($formData);
return true;
}
#[GetMapping(path: 'find/{id}/and/{in}')]
#[ApiOperation('查询单体记录')]
#[ApiHeader(name: 'test')]
public function find(int $id, float $in): array
{
return ['$id' => $id, '$in' => $in];
}
}
public function index(#[RequestQuery] #[Valid] DemoQuery $request){}
class DemoQuery
{
#[ApiModelProperty('名称')]
#[Required]
#[Max(5)]
#[In(['qq','aa'])]
public string $name;
#[ApiModelProperty('正则')]
#[Str]
#[Regex('/^.+@.+$/i')]
#[StartsWith('aa,bb')]
#[Max(10)]
public string $email;
#[ApiModelProperty('数量')]
#[Required]
#[Integer]
#[Between(1,5)]
public int $num;
}
use Hyperf\DTO\Annotation\Dto;
#[Dto]
class DemoQuery
{
}
use Hyperf\DTO\Annotation\Dto;
use Hyperf\DTO\Annotation\JSONField;
#[Dto]
class DemoQuery
{
#[ApiModelProperty('这是一个别名')]
#[JSONField('alias_name')]
#[Required]
public string $name;
}
use Hyperf\Serializer\SerializerFactory;
use Hyperf\Serializer\Serializer;
return [
Hyperf\Contract\NormalizerInterface::class => new SerializerFactory(Serializer::class),
];