PHP code example of wayhood / hyperf-apidocs
1. Go to this page and download the library: Download wayhood/hyperf-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/ */
wayhood / hyperf-apidocs example snippets
return [
// enable false 将不会启动 swagger 服务
'enable' => env('APP_ENV') !== 'prod',
'output_dir' => BASE_PATH . '/runtime/swagger',
//认证api key
'security_api_key' => ['Authorization'],
//全局responses
'responses' => [
401=>['description'=>'Unauthorized']
],
// swagger 的基础配置
'swagger' => [
'swagger' => '2.0',
'info' => [
'description' => 'swagger api desc',
'version' => '1.0.0',
'title' => 'API DOC',
],
'host' => '',
'schemes' => [],
],
];
public function add(#[RequestBody] DemoBodyRequest $request){}
public function add(#[RequestQuery] DemoQuery $request){}
public function fromData(#[RequestFormData] DemoFormData $formData){}
#[ApiFormData(name: 'photo', type: 'file')]
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}
#[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')]
#[ApiResponse(code: '200', description: 'success', className: Address::class, type: 'array')]
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;
}
#[Attribute(Attribute::TARGET_PROPERTY)]
class Image extends BaseValidation
{
protected $rule = 'image';
}
/**
* 映射数组.
* @var \App\DTO\Address[]
*/
#[ApiModelProperty('地址')]
public array $addressArr;
bash
php bin/hyperf.php vendor:publish tangwei/apidocs
shell script
php bin/hyperf.php start
[INFO] Swagger Url at 0.0.0.0:9501/swagger
[INFO] TaskWorker#1 started.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501