codag/restfabrication-bundle
Installation:
composer require codag/restfabrication-bundle:dev-master
Enable in config/bundles.php:
Codag\RestFabricationBundle\CodagRestFabricationBundle::class => ['all' => true],
Configure:
Add to config/packages/fos_rest.yaml:
restfabrication:
resource_dir: '%kernel.project_dir%/src/Resources/config/resources'
serializer:
groups: ['api']
First Use Case:
Create a YAML resource definition in config/resources/api_user.yml:
api_user:
type: entity
class: App\Entity\User
repository: App\Repository\UserRepository
routes:
get: /api/users/{id}
post: /api/users
put: /api/users/{id}
delete: /api/users/{id}
serializer:
groups: ['api']
Run php bin/console restfabrication:generate:controller to scaffold a CRUD controller.
Resource Definition:
Define API endpoints in YAML files under resource_dir (e.g., api_user.yml).
Example for nested relationships:
api_post:
type: entity
class: App\Entity\Post
routes:
get: /api/posts/{id}
post: /api/posts
serializer:
groups: ['api']
relations:
author:
type: entity
class: App\Entity\User
serializer:
groups: ['api']
Controller Generation: Generate controllers dynamically:
php bin/console restfabrication:generate:controller --resource=api_user
For custom actions, extend the generated controller:
// src/Controller/UserController.php
public function customAction(Request $request, $id) {
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
return $this->handleView($this->view(['custom_data' => 'value'], 200));
}
Validation: Use Symfony Validator constraints in YAML:
api_user:
validation:
name: NotBlank
email: Email
Event Listeners:
Attach listeners to resource events (e.g., prePersist):
api_user:
listeners:
prePersist: App\EventListener\UserPrePersistListener
Pagination: Enable in YAML:
api_user:
pagination: true
FOS\RestBundle\FOSRestBundle is enabled and configured for JSON responses.api serialization groups in your entities:
#[Groups(['api'])]
private $name;
api_user:
serializer:
class: App\Serializer\UserSerializer
RestFabricationBundle's test utilities:
$client = static::createClient();
$client->request('GET', '/api/users/1');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
Outdated Bundle:
fos/rest-bundle, symfony/framework-bundle).YAML Overrides:
@ORM\Table).Circular References:
api) must be defined to avoid circular reference errors.exclude_circular_reference: true in config/packages/serializer.yaml.Route Conflicts:
_format or _locale suffixes in YAML routes:
routes:
get: /api/users/{id}_{_format}
Doctrine Lifecycle Callbacks:
prePersist/preUpdate listeners in YAML do not trigger Doctrine events.Generated Controllers:
src/Controller/ (e.g., UserController.php).--dry-run flag to preview changes:
php bin/console restfabrication:generate:controller --resource=api_user --dry-run
Serialization Issues:
Serializer errors.bin/console debug:serializer to inspect groups.Route Debugging:
php bin/console debug:router | grep api_user
Custom Generators:
Codag\RestFabricationBundle\Generator\Generator to add custom templates.generate:service command for DTOs.Dynamic Resource Loading:
Codag\RestFabricationBundle\DependencyInjection\Compiler\ResourcePass to load resources from a database.API Versioning:
api_platform or api route prefixes in YAML:
routes:
get: /api/v1/users/{id}
Swagger/OpenAPI:
nelmio/api-doc-bundle by extending the generator to include annotations:
api_user:
swagger:
tags: ['Users']
description: 'User resource'
How can I help you explore Laravel packages today?