ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## Parameter请求类型 参数`@SWG\Parameter()`是配合``@SWG\Post()``,``@SWG\Get()``,``@SWG\Delete()``,``@SWG\Put()``使用设置请求参数的。 ``` /* @SWG\Post( * path="/index/register/index", * tags={"用户相关"}, * summary="用户注册", * @SWG\Parameter( * in="formData", * required=false, * name="simg", * type="file", * format="string", * description="description", * ), * / ``` | 参数 |含义 | | --- | --- | | ref | 允许此路径项的外部定义 | | in | 参数的位置,可设置的值为“query”、“header”、“path”、“formData”或“body” | | description | 描述 | | required | 确定此参数是否是必需的值可设置的值为 true 或 false | | type | 参数的类型,可设置值是“string”、“number”、“integer”、“boolean”、“array”或“file” | | format | 类型的扩展格式 | | items | 描述Array上的项目类型 | | collectionFormat | 数组的格式, | | default |设置参数的默认值。值的类型取决于定义的类型 | | maximum |最大长度 | | maximum |最小长度 | 下面我们来说下该怎么使用的`@SWG\Parameter()`里面的参数: > 当我们把```in```设置为“query”、“header”、“path”、“formData” > 示例: ``` /* @SWG\Post( * path="/index/register/index", * tags={"用户相关"}, * summary="用户注册", * @SWG\Parameter( * in="formData", * required=false, * name="simg", * type="file", * format="string", * description="description", * ), * @SWG\Parameter( * in="formData", * name="num", * type="integer", * maximum="10", * ), * @SWG\Response(response="200",description="ok") * * ), * / ``` > 需要注意的是参数`items`和 `collectionFormat` 只有参数type=“array”才使用: ``` /** @SWG\Parameter( * in="formData", * name="ids", * type="array", * items={"type","string"}, * collectionFormat="multi" * ), * */ ``` > 参数`collectionFormat` 是数组传递到API接口值得格式:csv-逗号分隔值、ssv-空格分隔值、tsv 以“%09”分隔值、pipes以“|”分隔值 、multi如果`in`选择”query“正常接收就好,如果选择“formdata”逗号分隔值 > 当参数 `in`选择“body”时必须定义`@SWG\Schema()`或`schema`,通过`@SWG\Schema()`自定义一个JSON > ``` /* * @SWG\Post( * path="/index/index/regiser", * tags={"用户相关"}, * summary="用户注册", * consumes={"application/json"}, * produces={"application/json"}, * @SWG\Parameter( * in="body", * required=true, * name="body", * description="description", * @SWG\Schema( * @SWG\Property( * property="firstName", * type="string", * ), * @SWG\Property( * property="lastName", * type="string" * ), * @SWG\Property( * property="username", * type="string" * ) * ), * ), * @SWG\Response(response="200",description="ok",) * ) */ ``` > 关于`@SWG\Schema()`的相关介绍可以查看Schema参数说明