PHPackages                             futuretek/openapi-gen-server-yii2 - 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. futuretek/openapi-gen-server-yii2

ActiveLibrary[API Development](/categories/api)

futuretek/openapi-gen-server-yii2
=================================

OpenAPI server stub generator for Yii2

1.9.0(1mo ago)0203↓22.2%MITPHPPHP &gt;=8.4CI passing

Since Mar 4Pushed 1mo agoCompare

[ Source](https://github.com/futuretek-solutions-ltd/openapi-gen-server-yii2)[ Packagist](https://packagist.org/packages/futuretek/openapi-gen-server-yii2)[ RSS](/packages/futuretek-openapi-gen-server-yii2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (11)Versions (15)Used By (0)

PHP OpenAPI Server Generator
============================

[](#php-openapi-server-generator)

[![Tests](https://github.com/futuretek-solutions-ltd/php-openapi-server-gen/actions/workflows/tests.yml/badge.svg)](https://github.com/futuretek-solutions-ltd/php-openapi-server-gen/actions/workflows/tests.yml)[![PHP 8.4+](https://camo.githubusercontent.com/b9e21a9ccd75d65db6edd4f091c534ac587a19d8e70f0f0764604e8d7a5d90f4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d254532253839254135382e342d3838393242462e737667)](https://php.net/)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](LICENSE)

A code generator that transforms an OpenAPI 3.0.x specification into a fully typed PHP server implementation for **Yii2**, with automatic request/response handling, body deserialization, and middleware support.

Key Features
------------

[](#key-features)

- **Schema DTOs** — Generates typed data classes with [`futuretek/data-mapper`](https://github.com/futuretek-solutions-ltd/data-mapper) attributes (`#[ArrayType]`, `#[MapType]`, `#[Format]`) and fluent setters for every property
- **Backed Enums** — PHP 8.4 string/int backed enums from OpenAPI enums, with `x-enum-descriptions` support
- **Controller Interfaces &amp; Abstract Controllers** — Generated into a `contracts` namespace (not `controllers`) to avoid conflicts with the Yii2 default controller namespace where your implementations live
- **Yii2 Route File** — Ready-to-use URL rules with type-based regex constraints (`\d+` for int/float, `\S+` for string)
- **Ambiguous Route Detection** — Warns when a `\S+` parametric route would shadow a static route in Yii2's ordered URL matching
- **File Upload Handling** — Single files and file arrays (`format: binary`) mapped to PSR-7 `UploadedFileInterface`, with a built-in `Psr7Stream` implementation for `getStream()`
- **Binary/Plain Response Types** — `application/octet-stream` responses return `UploadedFileInterface`, `text/plain` returns `string` — no spurious DTO classes generated
- **Array Request Bodies** — Typed array body parameters (`array` of DTOs) with `@param ItemClass[]` PHPDoc and `bodyIsArray` + `bodyItemClass` in `operationMeta`
- **Route Prefix** — Optional `routePrefix` for module-style routes (e.g. `api/pet/list-pets`)
- **Discriminator Mapping** — Polymorphic body deserialization via `oneOf`/`anyOf` with discriminator
- **Pluggable Middleware** — Authentication, authorization, logging, and file handling with sensible defaults
- **Namespace = Directory** — Files are placed according to their namespace, following Yii2 conventions
- **Strict Mode** — `--strict` flag treats warnings as errors (non-zero exit), suitable for CI pipelines

Requirements
------------

[](#requirements)

- PHP ≥ 8.4
- Composer

Installation
------------

[](#installation)

```
composer require futuretek/php-openapi-server-gen --dev
```

Quick Start
-----------

[](#quick-start)

### 1. Generate code from your OpenAPI spec

[](#1-generate-code-from-your-openapi-spec)

```
# Run from your Yii2 project root (@app)
vendor/bin/openapi-gen generate path/to/openapi.json
```

This uses defaults: `--base-dir=.`, `--namespace=app\api`, route file at `config/routes.api.php`.

### 2. With custom namespaces

[](#2-with-custom-namespaces)

```
vendor/bin/openapi-gen generate api.yaml \
  --base-dir='.' \
  --namespace='app\modules\api' \
  --schema-ns='schemas' \
  --enum-ns='enums' \
  --controller-ns='contracts' \
  --route-file='config/routes.api.php' \
  --route-prefix='api'
```

The `--route-prefix` option prepends a prefix to route targets, useful for Yii2 module-style routing (e.g. `api/pet/list-pets` instead of `pet/list-pets`).

### 3. Implement the generated interfaces

[](#3-implement-the-generated-interfaces)

```
