php-openapi-generator

Getting Started

This guide gets you from an OpenAPI spec to generated PHP models and API code in a few minutes.

It targets teams that want a PHP-only workflow: a small dev dependency, generated code committed or regenerated in CI, and no extra JS codegen toolchain.

1) Install the package

composer require --dev maxbeckers/php-openapi-generator

Allow the Composer plugin in your root composer.json:

{
  "config": {
    "allow-plugins": {
      "maxbeckers/php-openapi-generator": true
    }
  }
}

2) Add php-openapi-generator.php

Create this config file in your project root:

<?php

declare(strict_types=1);

use MaxBeckers\OpenApiGenerator\Config\FrameworkTarget;
use MaxBeckers\OpenApiGenerator\Config\GenerationTarget;
use MaxBeckers\OpenApiGenerator\Config\GeneratorConfig;
use MaxBeckers\OpenApiGenerator\Config\HttpClientAdapter;

$config = new GeneratorConfig();

$config->specFile = 'openapi.yaml';
$config->outputDir = 'generated';

$config->modelNamespace = 'App\\Model';
$config->modelOutputDir = 'Model';

$config->apiNamespace = 'App\\Api';
$config->apiOutputDir = 'Api';

$config->generationTarget = GenerationTarget::Server;
$config->frameworkTarget = FrameworkTarget::None;

// Optional when framework-specific majors need different generated glue:
// $config->frameworkVersion = '8.0';

// Optional when generating clients and adapter majors need different code:
// $config->httpClient = HttpClientAdapter::Guzzle;
// $config->httpClientVersion = '7.8';

return $config;

3) Generate code

vendor/bin/openapi-gen

4) Pick your target style

Useful CLI overrides:

vendor/bin/openapi-gen --target=server --framework=none
vendor/bin/openapi-gen --target=server --framework=symfony
vendor/bin/openapi-gen --target=server --framework=laravel
vendor/bin/openapi-gen --target=client --http-client=symfony
vendor/bin/openapi-gen --target=client --http-client=guzzle
vendor/bin/openapi-gen --target=client --http-client=psr18

5) Integrate generated code

If you want a framework-first path, jump to:

6) Regenerate on spec changes

Run generation each time your OpenAPI spec changes. You can run it explicitly with vendor/bin/openapi-gen or rely on the Composer plugin hook in your workflow.

Next Docs