1. Go to this page and download the library: Download yiisoft/yii-swagger library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yiisoft / yii-swagger example snippets
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsHtml;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Yii\Swagger\Action\SwaggerUi;
use Yiisoft\Yii\Swagger\Action\SwaggerJson;
// Swagger routes
Group::create('/swagger', [
Route::get('')
->middleware(FormatDataResponseAsHtml::class)
->action(fn (SwaggerUi $swaggerUi) => $swaggerUi->withJsonUrl('/swagger/json-url')),
Route::get('/json-url')
->middleware(FormatDataResponseAsJson::class)
->action(SwaggerJson::class),
]),
use OpenApi\Attributes as OA;
#[OA\Info(title:"My first API", version:"1.0")]
class DefaultController {
// ...
}
use OpenApi\Attributes as OA;
#[OA\Get(
path: "/api/endpoint",
summary: "Get default endpoint",
responses: [
new OA\Response(response: "200", description: "Get default action"),
],
)]
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// ...
}
//...
'yiisoft/yii-swagger' => [
'source-paths' => [
'@src/Controller' // Directory where annotations are used
],
'cacheTTL' => 60 // Enables caching and sets TTL, "null" value means infinite cache TTL.
],
//...