# yii-swagger
**Repository Path**: mirrors_yiisoft/yii-swagger
## Basic Information
- **Project Name**: yii-swagger
- **Description**: Swagger integration for Yii
- **Primary Language**: Unknown
- **License**: BSD-3-Clause
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-09-26
- **Last Updated**: 2026-05-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Yii Swagger
[](https://packagist.org/packages/yiisoft/yii-swagger)
[](https://packagist.org/packages/yiisoft/yii-swagger)
[](https://github.com/yiisoft/yii-swagger/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/yii-swagger)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/yii-swagger/master)
[](https://github.com/yiisoft/yii-swagger/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/yii-swagger)
OpenAPI Swagger for Yii Framework.
## Requirements
- PHP 8.1 - 8.5.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/yii-swagger
```
## Configuration
### 1. Add route configuration to `config/routes.php`
```php
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),
]),
```
### 2. Add attributes to default API controller
```php
use OpenApi\Attributes as OA;
#[OA\Info(title:"My first API", version:"1.0")]
class DefaultController {
// ...
}
```
and before actions
```php
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
{
// ...
}
```
See [Swagger-PHP documentation](https://zircote.github.io/swagger-php/guide/attributes.html) for details
on how to annotate your code.
### 3. Configure `SwaggerJson` action
For attributes to be registered you need to configure `SwaggerJson`.
You can use the parameters in `config/params.php` to configure `SwaggerJson`:
```php
//...
'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.
],
//...
```
### 4. (Optional) Add config for aliases and asset manager
```php
use Yiisoft\Definitions\Reference;
use Yiisoft\Assets\AssetManager;
return [
//...
'yiisoft/aliases' => [
'aliases' => [
//...
'@views' => '@root/views',
'@assets' => '@public/assets',
'@assetsUrl' => '@baseUrl/assets',
],
],
'yiisoft/view' => [
'basePath' => '@views',
'defaultParameters' => [
'assetManager' => Reference::to(AssetManager::class),
]
],
//...
];
```
### 5. (Optional) Configure `SwaggerUi` action
You can use the parameters in `config/params.php` to configure `SwaggerUi`.
For example, you can enable persisting authorization by setting the `persistAuthorization` parameter to `true`.
```php
//...
'yiisoft/yii-swagger' => [
'ui-params' => [
'persistAuthorization' => true,
],
],
//...
```
You can find a complete list of parameters by [following the link](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/).
### 6. (Optional) Configure `SwaggerService`
You can specify options for generation an `OpenApi\Annotations\OpenApi`
instance in `config/params.php` to configure `SwaggerService`:
```php
//...
'yiisoft/yii-swagger' => [
// Default values are specified.
'options' => [
'aliases' => [],
'namespaces' => [],
'config' => [],
'validate' => true,
'version' => OpenApi\Annotations\OpenApi::DEFAULT_VERSION,
],
],
//...
```
Or you can specify definition of `OpenApi\Generator` in `di-web.php`:
```php
use Yiisoft\Yii\Swagger\Service\SwaggerService;
return [
Generator::class => [
'setAnalyser' => [myAnalyser()],
],
//...
]
```
For more information about generation an `OpenApi\Annotations\OpenApi` instance, see the
documentation of the [zircote/swagger-php](https://github.com/zircote/swagger-php) package.
## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
## License
The Yii Swagger is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.
Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)