creads/api2symfony
Converts API Platform endpoints into Symfony-friendly client code from your OpenAPI/Swagger spec. Generates models and request classes to speed up integration with API Platform services and keep your Symfony app’s API clients consistent and maintainable.
Install the Package
composer require creads/api2symfony
(Note: Due to archival, verify compatibility with your Symfony version.)
Prepare OpenAPI/Swagger Spec
Ensure your api_spec.yaml or api_spec.json is valid and follows OpenAPI 2.0/3.x standards.
Generate Initial Code
vendor/bin/api2symfony generate --spec=path/to/api_spec.yaml --output=src/
(Default output: src/Controller/, src/Client/, src/DTO/)
First Use Case: API Server Stub
src/Controller/ to find auto-created controllers (e.g., UserController).GET /users/{id}) with Symfony’s built-in server:
php bin/console server:run
src/Controller/ → Symfony controllers with route annotations.src/Client/ → Typed API clients (e.g., UserApiClient).src/DTO/ → Data Transfer Objects (DTOs) with validation rules.config/packages/api2symfony.yaml (if auto-generated) for custom templates or overrides.Spec-First Development
api_spec.yaml → Regenerate code:
vendor/bin/api2symfony regenerate --spec=updated_spec.yaml
src/ (avoid merging conflicts by using --force sparingly).Controller Integration
// src/Controller/UserController.php (generated)
namespace App\Controller;
use App\DTO\UserDTO;
class UserController extends AbstractController {
public function getUser(UserDTO $user): UserDTO {
// Add custom logic here
return $user; // Auto-serialized to JSON
}
}
Client Usage
use App\Client\UserApiClient;
class UserService {
public function __construct(private UserApiClient $client) {}
public function fetchUser(int $id): array {
return $this->client->getUser($id)->toArray();
}
}
Validation & Serialization
# api_spec.yaml
definitions:
User:
properties:
email:
type: string
format: email
(Generates UserDTO with Symfony Validator constraints.)composer.json includes:
"require": {
"symfony/framework-bundle": "^5.0|^6.0",
"symfony/validator": "^5.0|^6.0"
}
config/api2symfony/templates/ to modify generated code (e.g., add traits).$client = new UserApiClient('http://api.test');
$response = $client->getUser(1);
$this->assertEquals('john@example.com', $response->email);
Archived Package Risks
symfony/yaml, symfony/validator).Regeneration Overwrites
regenerate without --force fails if files were manually modified.
Tip: Use git diff to track changes or stash modifications before regenerating.Circular Dependencies
User references Order, which references User) may break generation.
Fix: Simplify the spec or post-process generated DTOs.Validation Gaps
@assert) may not generate correctly.
Tip: Manually add constraints to DTOs after generation.var/log/dev.log or run with --verbose:
vendor/bin/api2symfony generate --verbose
config/routes.yaml:
controllers:
resource: ../src/Controller/
type: annotation
$validator = $this->container->get('validator');
$errors = $validator->validate($dto);
Custom DTO Mappers
Override src/DTO/AbstractDTO.php to add global methods (e.g., toArray()):
abstract class AbstractDTO {
public function toArray(): array {
return (new ArrayTransformer())->transform($this);
}
}
Event Listeners Attach listeners to generated controllers for pre/post-processing:
# config/services.yaml
services:
App\EventListener\ApiEventListener:
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
Template Overrides
Copy vendor/creads/api2symfony/templates/ to config/api2symfony/templates/ and modify:
Controller.twig → Add middleware or annotations.DTO.twig → Extend validation or add methods.API Client Extensions Extend generated clients to add retry logic or logging:
class CustomUserApiClient extends UserApiClient {
public function getUser(int $id): UserDTO {
$response = parent::getUser($id);
$this->logRequest($response);
return $response;
}
}
--only=controllers or --only=clients to regenerate specific parts.# .github/workflows/regenerate.yml
jobs:
regenerate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: composer install
- run: vendor/bin/api2symfony regenerate --spec=api_spec.yaml
- run: git diff --exit-code
How can I help you explore Laravel packages today?