Installation
Add the bundle to your composer.json:
composer require baconmanager/rest-bundle
Enable it in config/bundles.php:
BaconManager\RestBundle\BaconRestBundle::class => ['all' => true],
First Use Case: Basic REST Endpoint
Extend BaconManager\RestBundle\Controller\RestController in your own controller:
use BaconManager\RestBundle\Controller\RestController;
class PostController extends RestController
{
public function getListAction()
{
return $this->handleList($this->getDoctrine()->getRepository('App:Post')->findAll());
}
public function getItemAction($id)
{
return $this->handleItem($this->getDoctrine()->getRepository('App:Post')->find($id));
}
}
Routing
Define routes in config/routes.yaml:
bacon_rest_post:
resource: "@BaconRestBundle/Resources/config/routing.yml"
prefix: /api/posts
Serialization
Ensure jms/serializer-bundle is configured (default setup works out-of-the-box).
CRUD Operations Leverage built-in methods:
// List all items
$this->handleList($repository->findAll());
// Get single item
$this->handleItem($repository->find($id));
// Create/update/delete
$this->handleCreate($data);
$this->handleUpdate($id, $data);
$this->handleDelete($id);
Custom Serialization Override serialization logic in your controller:
public function getItemAction($id)
{
$item = $this->getDoctrine()->getRepository('App:Post')->find($id);
$view = $this->view($item, 200);
$view->setSerializationContext([
'groups' => ['post:read']
]);
return $this->handleView($view);
}
Validation
Use Symfony’s validator with handleCreate/handleUpdate:
$this->handleCreate($data, [
new Assert\NotBlank(),
new Assert\Type('string')
]);
Pagination
Integrate with knplabs/knp-paginator-bundle:
$paginator = $this->get('knp_paginator');
$collection = $paginator->paginate(
$repository->findAll(),
$this->get('request')->query->get('page', 1),
10
);
return $this->handleList($collection);
Authentication Secure endpoints via Symfony’s security component:
# config/routes.yaml
bacon_rest_post:
resource: "@BaconRestBundle/Resources/config/routing.yml"
prefix: /api/posts
defaults: { _security: "is_granted('ROLE_USER')" }
Dependency Conflicts
friendsofsymfony/rest-bundle:1.7.8 and jms/serializer-bundle:1.1.0. Ensure compatibility with your Symfony version (tested on Symfony 2.x).composer why-not baconmanager/rest-bundle to check conflicts.Missing Documentation
handleList, handleItem, etc.) are undocumented.BaconManager\RestBundle\Controller\RestController for method signatures and behavior.Serialization Groups
jms/serializer-bundle, ensure your entities have @Groups annotations:
use JMS\Serializer\Annotation as Serializer;
class Post
{
/**
* @Serializer\Groups({"post:read"})
*/
private $title;
}
Error Handling
try {
return $this->handleCreate($data);
} catch (\Exception $e) {
return $this->view(['error' => $e->getMessage()], 400);
}
Enable Debug Mode
Set APP_DEBUG=true in .env to see detailed errors.
Check Request/Response Dump request/response in controllers:
$request = $this->get('request');
dump($request->getContent());
Validate Serialization Test serialization separately:
$serializer = $this->get('jms_serializer');
$serialized = $serializer->serialize($item, 'json');
Custom Views
Override handleView to modify responses:
protected function handleView(View $view)
{
$view->setData([
'data' => $view->getData(),
'meta' => ['timestamp' => time()]
]);
return parent::handleView($view);
}
Add Middleware
Extend the bundle’s RestListener (in EventListener/RestListener.php) to add pre/post-processing logic.
Custom Formatters
Register new formatters (e.g., XML) by extending BaconManager\RestBundle\Formatter\JsonFormatter.
Event Dispatching
Listen to rest.pre_handle and rest.post_handle events for cross-cutting concerns:
// src/EventListener/CustomRestListener.php
class CustomRestListener
{
public function onPreHandle(GetResponseForControllerResultEvent $event)
{
// Logic here
}
}
How can I help you explore Laravel packages today?