fgh151/yii2-swagger

yii2 swagger files generator

Installs: 31

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

0.0.4 2024-07-08 10:14 UTC

This package is auto-updated.

Last update: 2024-11-22 08:57:31 UTC


README

Yii2 swagger generator

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist fgh151/yii2-swagger "*"

or add

"fgh151/yii2-swagger": "*"

to the require section of your composer.json file.

Usage

  1. Add module in config and set dirs with annotations
'modules' => [
    'swagger' => [
        'class' => fgh151\swagger\Module::class,
        'sources' => [
            dirname(__DIR__).'/controllers',
            dirname(__DIR__).'/models',
        ]
    ],
],
  1. Add routes
'rules' => [
    '/swagger/doc.json' => 'swagger/swagger/doc',
    '/swagger/ui' => 'swagger/swagger/ui',
],
  1. Add controller. It can be not useful and has random name, but contain annotations. Example:
<?php

namespace app\controllers;

use OpenApi\Attributes\Info;
use OpenApi\Attributes\OpenApi;
use OpenApi\Attributes\Server;
use yii\web\Controller;

#[OpenApi(
    info: new Info(version: '1.0.0', title: 'Super API title'),
)]
#[Server(url: 'https://api.example.com', description: 'Super API description')]
class SwaggerController extends Controller
{
}
  1. Add annotations to controllers and models. Example:
class SomeController extends \yii\web\Controller {
    #[Get(path: '/magic', summary: 'Magic API method.')]
    public function someAction() {
       //Magic here
    }
}
#[Schema(title: 'MyModel', description: 'Magic mode', properties: [
    new Property(property: 'Id', description: 'Идентификатор', type: 'integer'),
])]
class MyModel extends ActiveRecord
{
}

See annotations