REST API
Beginner
1 min read
Generating Docs with Swagger UI and Redoc
Example
# Install in Laravel
# composer require darkaonline/l5-swagger
# php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"
# config/l5-swagger.php key settings:
# 'documentations' => [
# 'default' => [
# 'api' => [ 'title' => 'My API' ],
# 'routes' => [ 'api' => 'api/documentation' ],
# ]
# ]
# Annotation-based approach (in Controller)
/**
* @OA\Get(
* path="/products",
* summary="List products",
* tags={"Products"},
* security={{ "bearerAuth":{} }},
* @OA\Parameter(name="status", in="query", @OA\Schema(type="string")),
* @OA\Response(
* response=200, description="OK",
* @OA\JsonContent(ref="#/components/schemas/ProductList")
* ),
* @OA\Response(response=401, description="Unauthenticated")
* )
*/
public function index(Request $request): JsonResponse { ... }
# Generate the spec:
# php artisan l5-swagger:generate
# Visit: http://localhost:8000/api/documentation
# Serve static Swagger UI from a YAML file (any stack):
# docker run -p 8080:8080 \
# -e SWAGGER_JSON=/spec/openapi.yaml \
# -v $(pwd)/docs:/spec \
# swaggerapi/swagger-ui
# Visit: http://localhost:8080