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

Domain Driven Bundle Laravel Package

dbrekelmans/domain-driven-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Install the Package Add the bundle via Composer in your Laravel project (note: this package is Symfony-focused, but can be used alongside Laravel via Symfony components or bridges):

    composer require dbrekelmans/domain-driven-bundle
    
  2. Configure the Bundle Create a config/packages/domain_driven.yaml file in your Laravel project (or adapt your existing Symfony config if using both frameworks). Use the default structure:

    domain_driven:
        directories:
            context: '%kernel.project_dir%/src'
            application: 'Application'
            domain: 'Domain'
            infrastructure: 'Infrastructure'
            presentation: 'Presentation'
            config: 'config'
        files:
            routes: 'routes'
            services: 'services'
    
  3. Enable Routing Add the following to your config/routes.yaml (Symfony) or adapt for Laravel’s routing system (e.g., via a bridge like spatie/laravel-symfony-support):

    framework:
        resource: '@DomainDrivenBundle/Resources/config/routes.yaml'
    
  4. Create a Domain Context Manually create a directory structure under src/ following the bundle’s conventions:

    src/
    └── YourDomain/
        ├── Application/
        ├── Domain/
        │   ├── Entity/
        │   ├── Event/
        │   ├── Repository/
        │   └── ...
        ├── Infrastructure/
        │   └── config/
        │       ├── routes.yaml
        │       └── services.yaml
        └── Presentation/
    
  5. Define a Route or Service Example routes.yaml in Infrastructure/config/routes.yaml:

    your_domain_homepage:
        path: /home
        controller: YourDomain\Presentation\Controller\HomeController::index
    
  6. Test the Integration Run your Symfony or Laravel application and verify the route/controller works. For Laravel, ensure you’ve bridged Symfony’s routing system (e.g., via a custom service provider or bridge package).


Implementation Patterns

Workflows for Daily Development

  1. Domain-Centric Development

    • Separate Contexts: Organize code by domain (e.g., src/User/, src/Order/). Each context follows the same Application/Domain/Infrastructure/Presentation structure.
    • Example Workflow:
      • Add a new entity in src/User/Domain/Entity/User.php.
      • Create a factory in src/User/Domain/Factory/UserFactory.php.
      • Define infrastructure services in src/User/Infrastructure/config/services.yaml:
        services:
            User\Infrastructure\Repository\UserRepository:
                arguments:
                    - '@doctrine.orm.entity_manager'
        
      • Expose a route in src/User/Infrastructure/config/routes.yaml:
        user_profile:
            path: /user/{id}
            controller: User\Presentation\Controller\UserController::show
        
  2. Service Configuration

    • Autoloading: The bundle auto-loads services.yaml/services.xml from Infrastructure/config/. Avoid manual services.yaml in config/ for domain-specific services.
    • Dependency Injection: Inject domain services into Laravel’s container via Symfony’s DI (e.g., using spatie/laravel-symfony-support):
      // In a Laravel service provider
      $this->app->singleton(
          User\Domain\Service\UserService::class,
          fn($app) => new User\Domain\Service\UserService(
              $app->make(User\Infrastructure\Repository\UserRepository::class)
          )
      );
      
  3. Routing Integration

    • Symfony Routes in Laravel: Use a bridge to load Symfony routes into Laravel’s router. Example with spatie/laravel-symfony-support:
      // config/app.php
      'providers' => [
          Spatie\SymfonySupport\SymfonySupportServiceProvider::class,
      ],
      
      Then configure the bundle to expose routes to Laravel’s router.
  4. Presentation Layer

    • Controllers: Place controllers in Presentation/Controller/. Example:
      // src/User/Presentation/Controller/UserController.php
      namespace User\Presentation\Controller;
      
      use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
      use Symfony\Component\HttpFoundation\Response;
      
      class UserController extends AbstractController {
          public function show(int $id): Response {
              return new Response('User ID: ' . $id);
          }
      }
      
    • Templates: Store Twig templates in Presentation/Resources/views/ (if using Twig in Laravel via symfony/twig-bundle).
  5. Domain Events

    • Event Dispatching: Dispatch domain events in Domain/Event/ and listen in Application/:
      // src/User/Domain/Event/UserRegistered.php
      class UserRegistered extends DomainEvent {}
      
      // src/User/Application/Listener/UserRegisteredListener.php
      class UserRegisteredListener {
          public function __invoke(UserRegistered $event) {
              // Handle event (e.g., send email)
          }
      }
      
    • Register listeners in services.yaml:
      services:
          User\Application\Listener\UserRegisteredListener:
              tags:
                  - { name: kernel.event_listener, event: User\Domain\Event\UserRegistered }
      
  6. Testing

    • Unit Tests: Test domain logic in Domain/ (e.g., entities, value objects).
    • Integration Tests: Test infrastructure/services in Infrastructure/.
    • Feature Tests: Test routes/controllers in Presentation/.

Integration Tips

  1. Laravel-Symfony Bridge Use spatie/laravel-symfony-support to bridge Symfony components (e.g., DI, routing) into Laravel:

    composer require spatie/laravel-symfony-support
    

    Configure the bundle to work alongside Laravel’s service container.

  2. Doctrine ORM If using Doctrine in Laravel (via laravel-doctrine), place repositories in Domain/Repository/ and configure them in services.yaml:

    services:
        User\Infrastructure\Repository\UserRepository:
            arguments:
                - '@doctrine.orm.entity_manager'
            tags:
                - { name: doctrine.repository_service }
    
  3. Custom Directories Override default directories in config/packages/domain_driven.yaml:

    domain_driven:
        directories:
            domain: 'Core'  # Use 'Core' instead of 'Domain'
            infrastructure: 'Backend'
    
  4. Excluding Contexts Disable auto-loading for specific contexts by excluding them in config or via a custom loader.

  5. Legacy Code Gradually migrate legacy code to the domain structure. Use the bundle’s auto-loading selectively for new features.


Gotchas and Tips

Pitfalls

  1. Symfony Dependency

    • The bundle requires Symfony components (e.g., symfony/framework-bundle). If your Laravel project doesn’t use Symfony, you’ll need to:
      • Install Symfony components as dependencies (e.g., symfony/http-foundation).
      • Use a bridge like spatie/laravel-symfony-support to integrate Symfony’s DI/routing into Laravel.
    • Workaround: Only use the bundle’s directory structure conventions without enabling auto-loading.
  2. Routing Conflicts

    • Symfony routes are not auto-loaded into Laravel’s router. You must manually bridge them (e.g., via spatie/laravel-symfony-support or a custom router).
    • Debugging: If routes don’t load, check:
      • The routes.yaml file exists in Infrastructure/config/.
      • The bundle is properly configured in config/packages/domain_driven.yaml.
      • Symfony’s router is integrated with Laravel’s (e.g., via middleware or a bridge).
  3. Service Container Conflicts

    • Laravel and Symfony’s service containers compete for control. Use spatie/laravel-symfony-support to merge them:
      // config/app.php
      'providers' => [
          Spatie\SymfonySupport\SymfonySupportServiceProvider::class,
      ],
      
    • Tip: Prefix Symfony services with symfony. to avoid conflicts:
      services:
          symfony.user_repository:
              class: User\Infrastructure\Repository\UserRepository
      
  4. Outdated Package

    • The package is archived (last release: 2019) and lacks Symfony 6.x support. Risks:
      • Bugs may not be fixed.
      • Incompatibility with newer Symfony/Laravel versions.
    • Mitigation:
      • Fork the package and update it for your needs.
      • Use it only for directory structure conventions (ignore auto-loading).
  5. Twig Integration

    • The bundle supports symfony/twig-bundle, but Laravel’s Blade templating may conflict. Options:
      • Use Twig only for specific domain contexts.
      • Stick to Blade and disable Twig auto-loading.
  6. Namespace Collisions

    • Domain contexts with similar names (e.g., User and `Admin
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor