API Documentation
Introduction
Clear and comprehensive API documentation is essential for effective communication between developers and consumers of your API. In this Booster, we utilize Swagger-PHP to annotate your PHP code and ReDoc to generate beautiful, interactive API documentation.
Why Swagger-PHP and ReDoc?
- Swagger-PHP: Allows you to describe your API endpoints, parameters, responses, and other details directly within your PHP code using annotations
- ReDoc: Generates stunning, interactive API documentation from your Swagger specification, making it easy for developers to explore and understand your API
Integrating Swagger-PHP and ReDoc
- Annotate Your Code:
- Add Swagger-PHP annotations to your PHP classes, methods, and properties to describe your API endpoints, parameters, responses, and other relevant information
- Refer to the Swagger-PHP documentation for detailed instructions on annotation syntax and usage.
- Generate the OpenAPI Specification:
- The
documentation/api.php
file in your project is responsible for generating theopenapi.yml
file, which contains the OpenAPI specification for your API - This file uses Swagger-PHP to scan your annotated code and generate the specification
- You can trigger the generation process using the following Composer script within your DDEV environment
composer generate-api-spec
- If you're using DDEV, you can run these commands within your DDEV environment:
ddev composer generate-api-spec
- The
- View Your API Documentation (Optional):
- While the
openapi.yml
file itself is machine-readable, you can use tools like ReDoc to generate human-readable, interactive documentation - Note: ReDoc integration is included in the Booster. Check out the
package.json
for thegenerate:api-doc:html
script, which uses ReDoc to generate the HTML documentation - Refer to the ReDoc documentation for more information.
- While the
Git Hooks Integration
The Booster's Git hooks are configured to automatically regenerate the API documentation whenever the openapi.yml
file is modified. This ensures that your documentation stays in sync with your code changes
Conclusion
Swagger-PHP and ReDoc provide a powerful combination for generating clear, comprehensive, and interactive API documentation. By annotating your code and automating the documentation generation process, you can ensure that your API is well-documented and easy to understand for both internal and external developers.
Additional Resources
Remember:
- Keep your annotations up-to-date as you make changes to your API
- Consider adding ReDoc or another visualization tool to make your API documentation even more accessible and user-friendly
- Use the generated OpenAPI specification to power other API-related tools and workflows.