Installation:
composer require dontdrinkandroot/rest-bundle
Add to config/bundles.php:
return [
// ...
DontDrinkAndRoot\RestBundle\DontDrinkAndRootRestBundle::class => ['all' => true],
];
Enable REST Endpoints:
Configure routing in config/routes.yaml:
dont_drink_and_root_rest:
resource: "@DontDrinkAndRootRestBundle/Resources/config/routing.yml"
prefix: /api
First Use Case:
Annotate a Doctrine entity (e.g., src/Entity/User.php) with @Rest\Resource:
use DontDrinkAndRoot\RestBundle\Annotation\Rest;
/**
* @Rest\Resource()
*/
class User {}
Access via /api/user (GET, POST, PUT, DELETE).
CRUD Operations: The bundle auto-generates REST endpoints for annotated entities. No manual controller needed. Example:
/**
* @Rest\Resource(
* collectionOperations={"get", "post"},
* itemOperations={"get", "put", "delete"}
* )
*/
class Product {}
GET /api/product → List all products.POST /api/product → Create a product.GET /api/product/{id} → Fetch a product.PUT /api/product/{id} → Update a product.Customization:
Override default behavior by extending the bundle’s services or creating custom controllers.
Example: Add a search operation:
# config/routes.yaml
dont_drink_and_root_rest.search:
path: /api/user/search
methods: GET
defaults: { _controller: App\Controller\UserController::searchAction }
Serialization:
Use JMS Serializer (bundled by default) to control output. Configure in config/packages/jms_serializer.yaml:
jms_serializer:
metadata:
directories:
App:
namespace_prefix: "App\\Entity"
path: "%kernel.project_dir%/config/serializer"
Authentication:
Secure endpoints with Symfony’s security component. Example in config/packages/security.yaml:
access_control:
- { path: ^/api/user, roles: ROLE_USER }
prePersist, preUpdate, etc., to modify data before serialization.@Assert\NotBlank) on entity properties.Outdated Dependencies: The bundle was last updated in 2016 and may conflict with modern Symfony/Laravel (if using via bridge). Test thoroughly with your stack.
symfony/dependency-injection to ^5.0).No Laravel Support: This is a Symfony bundle, not Laravel. For Laravel, consider:
Limited Documentation: Assumptions are made about Symfony’s internals (e.g., routing, DI). Refer to Symfony’s docs for edge cases.
Serialization Conflicts:
JMS Serializer may not handle circular references or complex types out-of-the-box. Configure custom handlers in config/packages/jms_serializer.yaml:
handlers:
App\Entity\ComplexType:
strategy: skip
Check Annotations:
Ensure @Rest\Resource is correctly placed. Use php app/console debug:container DontDrinkAndRoot\RestBundle\Annotation\Rest to verify registration.
Routing Debug:
Run php app/console debug:router to confirm REST routes are loaded.
Logs:
Enable debug mode (APP_ENV=dev) and check var/log/dev.log for serialization or Doctrine errors.
Custom Controllers:
Override default behavior by creating a controller and referencing it in collectionOperations/itemOperations:
/**
* @Rest\Resource(
* itemOperations={
* "custom" = { "method" = "GET", "path" = "/api/user/{id}/custom", "controller" = "App\Controller\UserController::customAction" }
* }
* )
*/
class User {}
Event Listeners:
Subscribe to rest.pre_serialization or rest.post_deserialization events to modify payloads:
// src/EventListener/CustomRestListener.php
class CustomRestListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'rest.pre_serialization' => 'onPreSerialization',
];
}
public function onPreSerialization(GetResponseEvent $event) { ... }
}
Dynamic Routing:
Use Symfony’s ParameterBag to dynamically adjust routes based on environment or user roles.
fetch="EAGER" or DQL JOIN to avoid N+1 queries in nested entities.
```markdown
---
**Note for Laravel Developers**:
While this bundle is Symfony-specific, its core concepts (auto-generated REST endpoints, JMS Serializer, Doctrine integration) align with Laravel’s ecosystem. For Laravel, consider:
- **Laravel: [Laravel API Resources](https://laravel.com/docs/eloquent-resources)** for serialization.
- **Laravel: [Dingo API](https://github.com/dingo/api)** for REST routing.
- **Laravel: [Fractal](https://fractal.thephpleague.com/)** for complex transformations.
How can I help you explore Laravel packages today?