I want to use swagger to document a laravel API and have users view the app using a similar route to the petstore.swagger.io
This are the steps that I have taken using swagger-php & l5-swagger packages
L5Swagger\L5SwaggerServiceProvider::class, to config/app.php filephp artisan l5-swagger:generateThen I added annotations to BookController like this
/**
* @OA\Info(
* version="1.0.0",
* title="Laravel Test OpenApi",
* description="L5 Swagger OpenApi description",
* @OA\Contact(
* email="[email protected]"
* ),
* @OA\License(
* name="Apache 2.0",
* url="http://www.apache.org/licenses/LICENSE-2.0.html"
* )
* )
*/
/**
* @OA\Server(
* url=L5_SWAGGER_CONST_HOST,
* description="L5 Swagger OpenApi dynamic host server"
* )
*
* @OA\Server(
* url="https://projects.dev/api/v1",
* description="L5 Swagger OpenApi Server"
* )
*/
class BookController extends Controller
{
/**
* @OA\Get(
* path="/books",
* operationId="getBooksList",
* tags={"Books"},
* summary="Get list of books",
* description="Returns list of books",
* @OA\Response(
* response=200,
* description="successful operation"
* ),
* @OA\Response(response=400, description="Bad request"),
* )
*
* Returns list of books
*/
public function index()
{
return BookResource::collection(Book::all());
}
}
At this point I don't know what url to use in loading the swagger UI to view the API documentation. This is the only thing holding me back right now in a coding challenge so I am a little bit desperate for help.
Phew...I was able to load swagger UI and view the api documentation using http://localhost:8000/api/documentation
I hope this will save someone else ample amount of time when using swagger for the first time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With