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.
composer require --dev maxbeckers/php-openapi-generator
Allow the Composer plugin in your root composer.json:
{
"config": {
"allow-plugins": {
"maxbeckers/php-openapi-generator": true
}
}
}
php-openapi-generator.phpCreate 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;
vendor/bin/openapi-gen
GenerationTarget::Server: generates API contracts from paths.GenerationTarget::Client: generates typed API client classes.$config->frameworkVersion for server framework majors and $config->httpClientVersion for client adapter majors.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
*ApiInterface methods.*ApiClient into your services.fromArray() for hydration and toArray() for serialization.If you want a framework-first path, jump to:
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.