定制 g4t/swagger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

g4t/swagger

Composer 安装命令:

composer require g4t/swagger

包简介

g4t laravel swagger

README 文档

README

The Swagger Laravel Autogenerate Package automatically generates OpenAPI (Swagger) documentation for your Laravel APIs from route definitions, validation rules, and optional controller inference—so you spend less time maintaining specs by hand.

I Stand with IRAN

Documentation: https://swagger.g4t.io/

Features

  • Mock workflow (experimental): optional swagger:mock-server.
  • Cached spec: swagger:cache and make:swagger.
  • Generates OpenAPI 3.x JSON from your registered routes (URI, methods, middleware, tags, etc.).
  • FormRequest rules drive request schemas and query/path parameters where applicable.
  • Optional inferred response examples from controllers (infer_response_examples): literals, JsonResource::collection / ::make / new Resource, response()->json(...), and more (see ResponseExampleExtractor).
  • Optional inferred error examples (infer_error_response_examples): response()->json([...], status), abort(), validation-style 422 when rules warrant it.
  • Faster JSON endpoint: when load_from_json is false, the package can serve public/{cached_spec_path} if it exists (use_cached_spec_when_present, default true), otherwise builds from routes on each request.
  • storage/swagger/… JSON files can override per-route response examples when enabled in config.
  • Route macros: ->description(), ->summary(), ->hiddenDoc(); controller #[SwaggerSection('…')].
  • Optional basic auth for the documentation UI (configurable).

I Stand with IRAN

Installation

composer require g4t/swagger

Usage

Video walkthrough

Explanatory video on how to use

  1. Publish the configuration file:

    php artisan vendor:publish --provider "G4T\Swagger\SwaggerServiceProvider"
  2. Adjust config/swagger.php (title, URL prefix, versions, inference flags, cached_spec_path / use_cached_spec_when_present, etc.).

  3. Open the UI at /{swagger.url} (default: /swagger/documentation), e.g. https://your-app.test/swagger/documentation.

  4. The issues page route is configured via swagger.issues_url (default: /swagger/issues).

  5. Describe a route:

    Route::get('user', [UserController::class, 'index'])->description('Get list of users with pagination.');
  6. Short summary on the route:

    Route::get('user', [UserController::class, 'index'])->summary('get users.');
  7. Hide an endpoint from the docs:

    Route::get('user', [UserController::class, 'index'])->hiddenDoc();
  8. Controller section description:

    <?php
    
    namespace App\Http\Controllers;
    
    use G4T\Swagger\Attributes\SwaggerSection;
    
    #[SwaggerSection('everything about your users')]
    class UserController extends Controller
    {
        // ...
    }
  9. Documentation basic auth (config/swagger.php):

    "enable_auth" => false,
    "username" => "admin",
    "password" => "pass",
    "sesson_ttl" => 100000,

Artisan commands

Command What it does
make:swagger Writes OpenAPI JSON to public/{cached_spec_path} (default doc.json). Legacy name; same destination as swagger:cache.
swagger:cache Regenerates and writes the cached spec to public/{cached_spec_path}, or swagger:cache --clear removes that file. Skips generation when swagger.enable is false (clear still works).
swagger:mock-server Optional G4T mock workflow (experimental).
php artisan make:swagger
php artisan swagger:cache
php artisan swagger:cache --clear
php artisan swagger:mock-server

Mock server (experimental)

G4T provides an optional command that connects your auto-generated API documentation to a hosted mock experience run by G4T. The goal is to make it easier to explore or demo your API surface while you build—without you having to host that side yourself.

Experimental: This feature is under testing. It may change, pause, or evolve without much advance notice. Do not rely on it for business-critical production paths yet.

Run:

php artisan swagger:mock-server

OpenAPI export & caching

make:swagger

Regenerates the spec from the current application routes and writes:

  • Path: public_path(config('swagger.cached_spec_path', 'doc.json'))
  • Encoding: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
php artisan make:swagger

Run after changing routes, controllers, FormRequests, or resources when you rely on a cached file for the JSON endpoint.

swagger:cache

Same write target and encoding as make:swagger, with an extra clear option:

php artisan swagger:cache
php artisan swagger:cache --clear

Use --clear to delete the cached file without regenerating. Regeneration requires swagger.enable to be true.

JSON endpoint behavior

The /{swagger.url}/json response is resolved by DocumentationController::resolveSwaggerSpecification():

load_from_json Behavior
true Only the file at public/{cached_spec_path} is used; missing/invalid file → empty array [].
false If use_cached_spec_when_present is true (default) and the file exists with valid JSON, that file is served; otherwise the spec is built live from routes.

Config / env (defaults apply if keys are missing from an older published config):

  • cached_spec_path — path under public/ (default doc.json; env SWAGGER_CACHED_SPEC_PATH).
  • use_cached_spec_when_present — default true; env SWAGGER_USE_CACHED_SPEC=false forces live generation without deleting the file.
  • load_from_json — static-file-only mode (legacy).

Suggestions

Feature requests: Canny board.

Contributing

Issues and pull requests are welcome on GitHub.

License

Open-source under the MIT license.

Credits

Maintained by HusseinAlaa.

Additional resources

g4t/swagger 适用场景与选型建议

g4t/swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.44k 次下载、GitHub Stars 达 124, 最近一次更新时间为 2023 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 g4t/swagger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 g4t/swagger 我们能提供哪些服务?
定制开发 / 二次开发

基于 g4t/swagger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 49.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 125
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 124
  • Watchers: 4
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-07