codingculture/doctrine-rest-bundle
Installation Add the package via Composer:
composer require codingculture/doctrine-rest-bundle
Register the bundle in config/bundles.php (Symfony):
CodingCulture\DoctrineRestBundle\CodingCultureDoctrineRestBundle::class => ['all' => true],
Configuration
Update config/packages/coding_culture_doctrine_rest.yaml with your Doctrine entity mappings:
coding_culture_doctrine_rest:
resources:
- { resource: 'App\Entity\Post', operations: ['get', 'post', 'put', 'delete'] }
First Use Case
Define a REST endpoint for a Post entity:
// src/Entity/Post.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Post {
#[ORM\Id, ORM\GeneratedValue, ORM\Column]
private ?int $id = null;
#[ORM\Column]
private string $title;
}
Access via /api/posts (default route prefix: /api).
CRUD Operations The bundle auto-generates REST endpoints for basic CRUD operations:
GET /api/posts → Fetch all posts.POST /api/posts → Create a new post.GET /api/posts/{id} → Fetch a single post.PUT /api/posts/{id} → Update a post.DELETE /api/posts/{id} → Delete a post.Customization via Annotations Override default behavior with annotations:
use CodingCulture\DoctrineRestBundle\Annotation as CR;
#[ORM\Entity]
#[CR\Rest("posts")]
class Post {
#[CR\Rest\Get]
public function getTitle(): string { return $this->title; }
#[CR\Rest\Post]
public function setTitle(string $title): void { $this->title = $title; }
}
Query Filtering
Enable filtering via ?filter[]=title=test:
coding_culture_doctrine_rest:
resources:
- { resource: 'App\Entity\Post', filters: ['title'] }
Serialization Groups Use Symfony Serializer for partial responses:
coding_culture_doctrine_rest:
resources:
- { resource: 'App\Entity\Post', serialization_groups: ['post:read'] }
Outdated Doctrine Support Last release in 2018 may conflict with modern Doctrine ORM (6.x+). Test thoroughly with your version.
No Built-in Authentication The bundle does not handle auth/permissions. Integrate with Symfony’s security layer:
# config/packages/security.yaml
access_control:
- { path: ^/api/posts, roles: ROLE_USER }
Limited Documentation Expect minimal docs. Refer to:
Route Conflicts
Default /api prefix may clash with other bundles. Customize in config/routes.yaml:
coding_culture_doctrine_rest:
resource: "@CodingCultureDoctrineRestBundle/Resources/config/routing.yml"
prefix: "/my-api"
Enable API Debugging Add a route to dump request/response:
// src/Controller/DebugController.php
use Symfony\Component\HttpFoundation\Response;
class DebugController {
public function debug(): Response {
return new Response(json_encode($_SERVER, JSON_PRETTY_PRINT));
}
}
Check Event Listeners Override bundle events for custom logic:
// src/EventListener/RestListener.php
use CodingCulture\DoctrineRestBundle\Event\RestEvent;
class RestListener {
public function onPreGet(RestEvent $event) {
if ($event->getEntity() instanceof Post) {
$event->setData(['custom' => 'value']);
}
}
}
Register in services.yaml:
services:
App\EventListener\RestListener:
tags:
- { name: kernel.event_listener, event: pre_get, method: onPreGet }
Custom Serializers
Extend CodingCulture\DoctrineRestBundle\Serializer\Serializer to add fields dynamically.
Validation Use Symfony Validator constraints:
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity]
class Post {
#[ORM\Column]
#[Assert\NotBlank]
private string $title;
}
Pagination Integrate with KnpPaginator or custom logic:
coding_culture_doctrine_rest:
resources:
- { resource: 'App\Entity\Post', pagination: { enabled: true, limit: 10 } }
How can I help you explore Laravel packages today?