eru123 / swagger
eru123/router 的 Swagger 插件
1.0.1
2023-04-01 03:33 UTC
Requires
- eru123/router: ^0.3.2
README
eru123/router 的 Swagger 插件
基本用法
<?php // Include the vendor/autoload.php file. require_once __DIR__ . '/vendor/autoload.php'; // Import the required classes. use eru123\swagger\Swagger; use eru123\router\Router; // Define Swagger path and it's json file path. $swagger = Swagger::build([ '/docs' => [ '/v1' => __DIR__ . '/swaggerv1.json', '/v2' => __DIR__ . '/swaggerv2.json', '/v3' => [ '-dev' => __DIR__ . '/swaggerv3-dev.json', '-prod' => __DIR__ . '/swaggerv3-prod.json', ] ] ]); // Create a new Router instance for the API path and add the Swagger instance as a child. $api = new Router(); $api->base('/api'); $api->child($swagger); // Create the main Router instance $router = new Router(); // Add a fallback route to return a 404 json response. $router->fallback('/', function () { return [ 'error' => '404 Not Found' ]; }); // Add the API router as a child of the main router. $router->child($api); // Run the router. $router->run();
生成的路由
上述示例代码将生成以下路由
- /api/docs/v1
- /api/docs/v2
- /api/docs/v3-dev
- /api/docs/v3-prod
对于这些路由中的每一个,Swagger::build() 方法指定的文件将作为 json 文件在这些路径上以 /swagger.json
的形式提供(例如 /api/docs/v1/swagger.json
)。