百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-swagger

> 后端框架
开源

Laravel-Swagger 是一个完全自动化的工具,可以在成功完成应用程序的功能测试后生成和保存基于 Swagger 的文档。

180 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Laravel-Swagger 是一个完全自动化的工具,可以在成功完成应用程序的功能测试后生成和保存基于 Swagger 的文档。

Laravel Swagger plugin

Comparison to another documentation generators

LaravelSwagger Scramble
Force developers to write tests :white_check_mark: :x:
Guarantee that API works :white_check_mark: :x:
Works with any route types covered by tests :white_check_mark: :x:
Generate response schema using JSON Resource class :x: :white_check_mark:
Runtime documentation generation :x: :white_check_mark:

Introduction

This plugin is designed to generate documentation for your REST API during the passing PHPUnit tests.

Installation

  1. Install the package using the following command: composer require ronasit/laravel-swagger

Note

For Laravel 5.5 or later the package will be auto-discovered. For older versions add the AutoDocServiceProvider to the providers array in config/app.php as follow:

php
'providers' => [
    ...
    RonasIT\AutoDoc\AutoDocServiceProvider::class,
],
  1. Run
php artisan vendor:publish --provider=RonasIT\\AutoDoc\\AutoDocServiceProvider
  1. Add \RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::class middleware to the global HTTP middleware list bootstrap\app.php:
php
    return Application::configure(basePath: dirname(__DIR__))
        ->withMiddleware(function (Middleware $middleware) {
            $middleware->use([
                ...
                \RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::class,
            ]);
        });
  1. Add \RonasIT\AutoDoc\Traits\AutoDocTestCaseTrait trait to tests/TestCase.php

  2. Configure documentation saving using one of the next ways:

  • Add SwaggerExtension to the `` block of your phpunit.xml. Please note that this way will be removed after updating PHPUnit up to 10 version (https://github.com/sebastianbergmann/phpunit/issues/4676)
xml

    
        
    
    
        
            ./tests/Feature
        
    
  • Call php artisan swagger:push-documentation console command after the tests stage in your CI/CD configuration

Usage

Basic usage

  1. Create request class:

    php
     'string',
                'is_active' => 'boolean',
                'age' => 'integer|nullable'
            ];
        }
    }
    

    Note

    For correct working of plugin you'll have to dispose all the validation rules in the rules() method of your request class. Also, your request class must be connected to the controller via dependency injection. Plugin will take validation rules from the request class and generate fields description of input parameter.

  2. Create a controller and a method for your route:

    php
     ***Note***
    > 
    > Dependency injection of request class is optional but if it not presents,
    > the "Parameters" block in the API documentation will be empty.
  3. Create test for API endpoint:

    php
    public function testUpdate()
    {
        $response = $this->json('put', '/users/1', [
            'name': 'Updated User',
            'is_active': true,
            'age': 22
        ]);
    
        $response->assertStatus(Response::HTTP_NO_CONTENT);
    }
  4. Run tests

  5. Go to route defined in the auto-doc.route config

  6. Profit!

Annotations

You can use the following annotations in your request classes to customize documentation of your API endpoints:

  • @summary - short description of request
  • @description - implementation notes
  • @_204 - custom description of response code. You can specify any code as you want.
  • @some_field - description of the field from the rules method
  • @deprecated - mark route as deprecated

Note

If you do not use request class, the summary and description and parameters will be empty.

Configs

  • auto-doc.route - route for generated documentation
  • auto-doc.basePath - root of your API

Custom driver

You can specify the way to collect and view documentation by creating your own custom driver.

You can find example of drivers here.

Viewing OpenAPI documentation

As of version 2.2, the package includes the ability to switch between OpenAPI documentation viewers. To access different viewers, modify the documentation_viewer configuration. This change is reflected immediately, without the need to rebuild the documentation file.

Merging additional documentations

The package supports the integration of the primary documentation with additional valid OpenAPI files specified in the additional_paths configuration.

Integration with Postman

The package provides a separate endpoint to get the OpenAPI spec documentation in a Postman-specific format. Pass your app's /auto-doc/documentation/postman endpoint into the Postman import.

Migration guides

3.0.1-beta

Contributing

Thank you for considering contributing to Laravel Swagger plugin! The contribution guide can be found in the Contributing guide.

License

Laravel Swagger plugin is open-sourced software licensed under the MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPautomationlaravelopenapiphp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架