Integrating swagger-php 2.0 with ThinkPHP to Generate API Documentation
This tutorial walks through installing swagger-php 2.0, downloading swagger-ui, integrating it into a ThinkPHP project, generating a swagger.json file via annotations, and provides code snippets and configuration details to help developers set up API documentation in a PHP backend environment.
During my study of Swagger I discovered that most online resources are incomplete, so I wrote this article to share my troubleshooting process and help others set up Swagger documentation in a PHP backend.
1. Install swagger-php 2.0 composer require zircote/swagger-php:2.0.* 2. Download swagger-ui
Download the static Swagger UI files from the official site and place them in the public directory of the ThinkPHP project (e.g., the public folder). https://swagger.io/tools/swagger-ui/ 3. Integrate swagger-php into ThinkPHP and generate swagger.json
use Swagger\Annotations as SWG;
class Index {
public function index() {
$swagger = \Swagger\scan(__DIR__);
$res = $swagger->saveAs('./swagger.json');
}
}4. Documentation annotation example
/**
* @SWG\Swagger(
* schemes={"http"},
* host="www.tp.com.cn/",
* basePath="/",
* @SWG\Info(
* title="API文档",
* version="1.0.0"
* )
* )
*/For a detailed guide on using swagger-php, refer to the following documentation:
https://www.kancloud.cn/chengguang/swagger-php#/catalogThe article also includes screenshots and a call‑to‑action to read the original online post for the full content.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
