Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rest Bundle Laravel Package

baconmanager/rest-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    
  2. 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));
        }
    }
    
  3. Routing Define routes in config/routes.yaml:

    bacon_rest_post:
        resource: "@BaconRestBundle/Resources/config/routing.yml"
        prefix: /api/posts
    
  4. Serialization Ensure jms/serializer-bundle is configured (default setup works out-of-the-box).


Implementation Patterns

Common Workflows

  1. 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);
    
  2. 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);
    }
    
  3. Validation Use Symfony’s validator with handleCreate/handleUpdate:

    $this->handleCreate($data, [
        new Assert\NotBlank(),
        new Assert\Type('string')
    ]);
    
  4. 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);
    
  5. 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')" }
    

Gotchas and Tips

Pitfalls

  1. Dependency Conflicts

    • The bundle requires friendsofsymfony/rest-bundle:1.7.8 and jms/serializer-bundle:1.1.0. Ensure compatibility with your Symfony version (tested on Symfony 2.x).
    • Fix: Use composer why-not baconmanager/rest-bundle to check conflicts.
  2. Missing Documentation

    • The bundle lacks detailed docs. Key methods (handleList, handleItem, etc.) are undocumented.
    • Workaround: Inspect BaconManager\RestBundle\Controller\RestController for method signatures and behavior.
  3. Serialization Groups

    • If using jms/serializer-bundle, ensure your entities have @Groups annotations:
      use JMS\Serializer\Annotation as Serializer;
      
      class Post
      {
          /**
           * @Serializer\Groups({"post:read"})
           */
          private $title;
      }
      
  4. Error Handling

    • The bundle throws generic exceptions. Customize error responses:
      try {
          return $this->handleCreate($data);
      } catch (\Exception $e) {
          return $this->view(['error' => $e->getMessage()], 400);
      }
      

Debugging Tips

  1. Enable Debug Mode Set APP_DEBUG=true in .env to see detailed errors.

  2. Check Request/Response Dump request/response in controllers:

    $request = $this->get('request');
    dump($request->getContent());
    
  3. Validate Serialization Test serialization separately:

    $serializer = $this->get('jms_serializer');
    $serialized = $serializer->serialize($item, 'json');
    

Extension Points

  1. Custom Views Override handleView to modify responses:

    protected function handleView(View $view)
    {
        $view->setData([
            'data' => $view->getData(),
            'meta' => ['timestamp' => time()]
        ]);
        return parent::handleView($view);
    }
    
  2. Add Middleware Extend the bundle’s RestListener (in EventListener/RestListener.php) to add pre/post-processing logic.

  3. Custom Formatters Register new formatters (e.g., XML) by extending BaconManager\RestBundle\Formatter\JsonFormatter.

  4. 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
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle
dmstr/api-platform-utils-bundle
dmstr/api-configuration-bundle
chrisdev/ux-components
baks-dev/finances
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle