PHPackages                             kaxiluo/swagger-laravel-code-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. kaxiluo/swagger-laravel-code-generator

ActiveLibrary[API Development](/categories/api)

kaxiluo/swagger-laravel-code-generator
======================================

Automatically generate laravel models, resources, controllers, routes based on swagger documents

1.0.0(4y ago)326MITPHPPHP &gt;=7.1.3

Since Aug 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/kaxiluo/swagger-laravel-code-generator)[ Packagist](https://packagist.org/packages/kaxiluo/swagger-laravel-code-generator)[ RSS](/packages/kaxiluo-swagger-laravel-code-generator/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (3)Used By (0)

swagger-laravel-code-generator
==============================

[](#swagger-laravel-code-generator)

根据swagger文档自动生成laravel模型，资源，控制器，路由

安装
--

[](#安装)

[Packagist](https://packagist.org/packages/kaxiluo/swagger-laravel-code-generator)

```
composer require kaxiluo/swagger-laravel-code-generator
```

使用方法
----

[](#使用方法)

`php artisan swagger-to-code:gen ./docs/your-openapi.yaml --ignored-schema-regular=^Error* --all --force`

参数：

- yaml文件的相对路径（相对于工程根目录）
- 可选参数 `--resource --controller --route --ignored-schema-regular= --force --all`

~ 运行一个例子试试 `php artisan swagger-to-code:gen ./vendor/kaxiluo/swagger-laravel-code-generator/example-swagger/example-openapi.yaml --ignored-schema-regular=^Error* --all`

### 模型 &amp;&amp; 资源

[](#模型--资源)

根据文档中的 `schemas` 生成对应的模型和资源类，默认不会生成User模型

可选参数：`--ignored-schema-regular=`

参数说明：忽略文档中模型的正则表达式，示例中 `--ignored-schema-regular=^Error*` 表示不生成以Error开头的模型和资源

举个栗子，如果swagger中定义有如下schema：

```
Article:
  type: object
  properties:
    id:
      type: integer
    title:
      type: string
      title: 文章标题
    cover:
      title: 文章封面图
      type: string
      format: uri
    published_time:
      type: string
      title: 文章发布时间
    author:
      $ref: '#/components/schemas/Author'
ArticleDetail:
  allOf:
    - $ref: '#/components/schemas/Article'
    - type: object
      properties:
        description:
          type: string
          title: html描述
        comments:
          type: array
          title: 文章评论
          items:
            $ref: '#/components/schemas/Comment'
Comment:
  type: object
  properties:
    nickname:
      type: string
      title: 评论者昵称
    content:
      type: string
      title: 评论内容
Author:
  type: object
  properties:
    id:
      type: integer
    nickname:
      type: string
      title: 作者昵称
```

生成的模型：`Article` `Comment` `Author`

生成的资源：

`app/Http/Resources/ArticleResource.php`

```
public function toArray($request)
{
    return [
        'id' => (int)$this->id,
        'title' => (string)$this->title,
        'cover' => (string)$this->cover,
        'published_time' => (string)$this->published_time,
        'author' => new AuthorResource($this->author),
    ];
}
```

`app/Http/Resources/AuthorResource.php`

```
public function toArray($request)
{
    return [
        'id' => (int)$this->id,
        'nickname' => (string)$this->nickname,
    ];
}
```

`app/Http/Resources/ArticleDetailResource.php`

```
class ArticleDetailResource extends ArticleResource
{
    public function toArray($request)
    {
        $baseInfo = parent::toArray($request);
        return array_merge($baseInfo, [
			'description' => (string)$this->description,
			'comments' => CommentResource::collection($this->comments),
        ]);
    }
}
```

`app/Http/Resources/CommentResource.php`

```
public function toArray($request)
{
    return [
        'nickname' => (string)$this->nickname,
        'content' => (string)$this->content,
    ];
}
```

### 控制器 &amp;&amp; 路由

[](#控制器--路由)

根据文档中 `paths` 定义的 `operationId` 生成对应的控制器和路由，如果没有定义`operationId`将不会生成控制器和路由

举个栗子，如果swagger中定义

```
/articles:
  get:
      summary: '获取文章列表'
      operationId: Article/ArticleController@index
```

生成的控制器：

`app/Controllers/Article/ArticleController.php` , 类中包含`index`方法

生成的路由：

`Route::get('/articles', 'Article\ArticleController@index');`

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1721d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/27921328?v=4)[linyiyong](/maintainers/linyiyong)[@linyiyong](https://github.com/linyiyong)

---

Top Contributors

[![kaxiluo](https://avatars.githubusercontent.com/u/32103283?v=4)](https://github.com/kaxiluo "kaxiluo (7 commits)")

---

Tags

swaggerphplaravelswaggercode generatorREST APIgenerateopenapi3dev-toolopenapi-specification

### Embed Badge

![Health badge](/badges/kaxiluo-swagger-laravel-code-generator/health.svg)

```
[![Health](https://phpackages.com/badges/kaxiluo-swagger-laravel-code-generator/health.svg)](https://phpackages.com/packages/kaxiluo-swagger-laravel-code-generator)
```

###  Alternatives

[darkaonline/l5-swagger

OpenApi or Swagger integration to Laravel

2.9k34.0M111](/packages/darkaonline-l5-swagger)[knuckleswtf/scribe

Generate API documentation for humans from your Laravel codebase.✍

2.3k12.2M45](/packages/knuckleswtf-scribe)[ronasit/laravel-swagger

Provided middleware for generating of swagger-documentation file by run testing of restful API.

179124.9k2](/packages/ronasit-laravel-swagger)[erasys/openapi-php

Open API 3.0 builder and validation library for PHP that helps you write valid specs.

2080.3k5](/packages/erasys-openapi-php)[johnylemon/laravel-apidocs

Laravel API documentation generating tool

472.8k](/packages/johnylemon-laravel-apidocs)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
