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

Doctrine Rest Bundle Laravel Package

codingculture/doctrine-rest-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    
  2. 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'] }
    
  3. 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).


Implementation Patterns

Workflows

  1. 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.
  2. 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; }
    }
    
  3. Query Filtering Enable filtering via ?filter[]=title=test:

    coding_culture_doctrine_rest:
        resources:
            - { resource: 'App\Entity\Post', filters: ['title'] }
    
  4. Serialization Groups Use Symfony Serializer for partial responses:

    coding_culture_doctrine_rest:
        resources:
            - { resource: 'App\Entity\Post', serialization_groups: ['post:read'] }
    

Gotchas and Tips

Pitfalls

  1. Outdated Doctrine Support Last release in 2018 may conflict with modern Doctrine ORM (6.x+). Test thoroughly with your version.

  2. 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 }
    
  3. Limited Documentation Expect minimal docs. Refer to:

    • Source Code for undocumented features.
    • Symfony’s REST best practices for gaps.
  4. 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"
    

Debugging Tips

  1. 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));
        }
    }
    
  2. 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 }
    

Extension Points

  1. Custom Serializers Extend CodingCulture\DoctrineRestBundle\Serializer\Serializer to add fields dynamically.

  2. Validation Use Symfony Validator constraints:

    use Symfony\Component\Validator\Constraints as Assert;
    
    #[ORM\Entity]
    class Post {
        #[ORM\Column]
        #[Assert\NotBlank]
        private string $title;
    }
    
  3. Pagination Integrate with KnpPaginator or custom logic:

    coding_culture_doctrine_rest:
        resources:
            - { resource: 'App\Entity\Post', pagination: { enabled: true, limit: 10 } }
    
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.
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
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui