如何使用laravel创建swagger json路线

时间:2018-02-27 09:51:01

标签: php laravel-5.5 swagger-2.0

我想在laravel中使用swagger创建这种类型的JSON,它怎么可能?

// will always be string
var array = ["a", "b", "c", "d"];

// Will always be number
var array2 = [1, 2, 0, 4, ];

var result = array.map((s, i) => [s, array2[i]]) //combine values
  .filter(s => s[1] != 0) //filter out 0s
  .map(s => s.join("")); //join them

console.log(result);

如何创建此路线:#/ definitions / CustomerAuthenticateResponse

2 个答案:

答案 0 :(得分:1)

我得到了使用swagger传递身体请求(json)的解决方案。

<强>控制器:

class abcController extends Controller
{
    /*
     * @SWG\Post(
     *      path="/api/user",
     *       tags={"User"},
     *      operationId="ApiV1saveUser",
     *      summary="Add User",
     *      consumes={"application/json"},
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          @SWG\Schema(ref="#/definitions/User"), //User Model
     *      ),
     *      @SWG\Response(
     *         response=200,
     *         description="Success"
     *      ),
     * )
     */
     public function store(Request $request)
     {
     }
}

用户模型:

/**
    * @SWG\Definition(
    *      required={"name","email"},
    *      type="object",
    *      @SWG\Xml(name="User")
    * ),
    * @SWG\Property(format="byte", type="string", property="name", description="User Name"),
    * @SWG\Property(format="byte", type="string",property="email", description="Email")
    */
    class User extends Model
    {
    }

谢谢,

答案 1 :(得分:0)

看看这个package 安装后,您可以将定义放在控制器的phpdoc中。

class PetController
{
    /**
     * @OAS\Post(
     *     path="/pet",
     *     tags={"pet"},
     *     summary="Add a new pet to the store",
     *     operationId="addPet",
     *     @OAS\Response(
     *         response=405,
     *         description="Invalid input"
     *     ),
     *     security={
     *         {"petstore_auth": {"write:pets", "read:pets"}}
     *     },
     *     requestBody={"$ref": "#/components/requestBodies/Pet"}
     * )
     */
    public function addPet()
    {
    }
}

source