thatobabusi / laravel-ddd
A Laravel toolkit for Domain Driven Design patterns
Requires
- php: ^8.3|^8.4
- illuminate/contracts: ^11.44|^12.0|^13.0
- laravel/pint: ^1.21
- laravel/prompts: ^0.3.1
- lorisleiva/lody: ^0.6|^0.7
- spatie/laravel-package-tools: ^1.19.0
- symfony/finder: ^7.0
- symfony/var-exporter: ^7.1
Requires (Dev)
- larastan/larastan: ^2.0.1|^3.0
- lorisleiva/laravel-actions: ^2.9.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^9.11|^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-data: ^4.11.1
Suggests
- lorisleiva/laravel-actions: Recommended for Actions.
- spatie/laravel-data: Recommended for Data Transfer Objects.
README
Laravel-DDD is a toolkit to support domain-driven design (DDD) in Laravel applications. One of the pain points when adopting DDD is the inability to use Laravel's native make commands to generate objects outside the App\* namespace. This package aims to fill the gaps by providing equivalent commands such as ddd:model, ddd:dto, ddd:view-model and many more.
Quick Start
composer require thatobabusi/laravel-ddd php artisan ddd:install php artisan ddd:config wizard
All-in-One Feature Wizard (NEW!)
Generate a complete DDD feature in seconds:
php artisan ddd:make:feature ForUserLogin --folder=Authentication
Generates (10+ files automatically):
- Request class with validation
- Action (invokable controller)
- UseCase (interface + implementation)
- Domain Service (interface + implementation)
- Repository (interface + implementation)
- Output DTO
- Response (interface + implementation)
- Auto-prints: Exact binding code for
AppServiceProvider::register() - Auto-prints: Exact route definition for
routes/api.php - All files include TODO markers showing exactly what to implement
See Quick Start Wizard for complete guide, real-world examples, and advanced options.
Scaffold a Bounded-Context (Traditional Approach)
Alternatively, use granular commands for full control:
php artisan ddd:make:domain UserManagement php artisan ddd:eloquent-model UserManagement:User php artisan ddd:repository UserManagement:UserRepository php artisan ddd:mapper UserManagement:UserMapper php artisan ddd:policy UserManagement:UserPolicy php artisan ddd:provider UserManagement:UserManagementServiceProvider php artisan ddd:command-query UserManagement:CreateUserCommand php artisan ddd:command-query UserManagement:GetUserQuery --query
See Scaffolding Guide for complete workflow.
Command Comparison
| Approach | Speed | Flexibility | Learning Curve |
|---|---|---|---|
Feature Wizard ddd:make:feature |
⚡ 2 min | Good (answer 9 questions) | Beginner |
Granular Commands ddd:* |
🐢 10 min | Excellent (build incrementally) | Intermediate |
Use the wizard for: Creating a feature from scratch quickly.
Use granular commands for: Fine-grained control, incremental building, or customization.
Documentation
Complete documentation is organized into focused guides:
Getting Started
- Installation & Setup — Install, initialize, and deploy.
- Configuration — Configure domains, layers, and namespaces.
Using the Toolkit
- Available Commands — Reference for all
ddd:*generators. - Advanced Usage — Nested objects, subdomains, custom resolvers, application layers.
- Scaffolding & Domain Structure — Bounded-contexts, layered architecture, CQRS patterns. NEW!
Building with DDD
- Real-World Examples — Complete working domains (Invoicing, Payment, testing).
- Events & Listeners — Event-driven architecture and decoupled domains.
- Migrating Existing Apps — Step-by-step guide to refactor traditional Laravel apps.
Customization & Optimization
- Customizing Stubs — Publish and override generated file templates.
- Autoloading & Discovery — Auto-register providers, commands, policies, factories, migrations, listeners.
- Performance & Optimization — Caching, query optimization, autoloading optimization.
Quality & Security
- Security Best Practices — Authorization, validation, encryption, audit logging.
- Testing — Testing domain objects and best practices.
Reference
- Version Compatibility — Laravel & PHP version matrix and upgrading.
- Troubleshooting — Common issues and solutions.
- FAQ — Frequently asked questions.
- Contributing — How to contribute stubs, features, or fixes.
Key Features
- ✅ All-in-one feature wizard:
ddd:make:featuregenerates complete DDD features with intelligent prompting - ✅ TODO-driven scaffolding: All generated files have clear markers showing what to implement
- ✅ Auto-bindings & routes: Wizard prints exact code for AppServiceProvider and routes/api.php
- ✅ 11 generators: domain, eloquent-model, mapper, repository, policy, provider, command, query, use-case, response, action
- ✅ Generate domain objects outside
App\*withddd:*commands - ✅ Support for nested objects and subdomains (dot notation)
- ✅ Configurable application layer (controllers, requests, middleware)
- ✅ Custom layers (Infrastructure, Integrations, etc)
- ✅ Auto-discovery of providers, commands, policies, factories, migrations, listeners
- ✅ Event-driven architecture with decoupled domains
- ✅ Production autoloading optimization
- ✅ Customizable stubs
Version Compatibility
| Laravel | LaravelDDD | PHP | Status |
|---|---|---|---|
| ^13.x | 4.x | 8.4+ | Current Experimental (active WIP MVP) |
| 11.x – 13.x | 3.x | 8.3+ | Latest Maintained by Jasper Tey - Laravel DDD |
| 11.44.x+ | 2.x | 8.3+ | Legacy Maintained by Jasper Tey - Laravel DDD |
| 9.x – 11.x | 0.x – 1.x | 8.1+ | Legacy |
See UPGRADING for migration details.
Example: Complete Feature (Feature Wizard)
# Interactive wizard php artisan ddd:make:feature ForCreatePost --folder=Blog # Or with options php artisan ddd:make:feature ForCreatePost \ --folder=Blog \ --with-entity \ --with-eloquent-model
Output:
✔ CREATED app/Http/Requests/Api/V1/Blog/ForCreatePostRequest.php
✔ CREATED app/Http/Controllers/Api/V1/Blog/ForCreatePostAction.php
✔ CREATED app/UseCases/Blog/IForCreatePostUseCase.php
✔ CREATED app/UseCases/Blog/ForCreatePostUseCase.php
✔ CREATED app/Domain/Blog/Services/IForCreatePostService.php
✔ CREATED app/Infra/Blog/Services/ForCreatePostService.php
✔ CREATED app/Domain/Blog/Repositories/IForCreatePostRepository.php
✔ CREATED app/Infra/Blog/Repositories/ForCreatePostRepository.php
✔ CREATED app/Domain/Blog/Services/Output/ForCreatePostOutput.php
✔ CREATED app/Http/Responses/Api/V1/Blog/IForCreatePostResponse.php
✔ CREATED app/Http/Responses/Api/V1/Blog/ForCreatePostResponse.php
── Add to AppServiceProvider::register() ─────────────────────
$this->app->bind(App\UseCases\Blog\IForCreatePostUseCase::class, ...);
$this->app->bind(App\Domain\Blog\Services\IForCreatePostService::class, ...);
$this->app->bind(App\Domain\Blog\Repositories\IForCreatePostRepository::class, ...);
$this->app->bind(App\Http\Responses\Api\V1\Blog\IForCreatePostResponse::class, ...);
── Add to routes/api.php ─────────────────────────────────────
Route::post('/for-create-post', \App\Http\Controllers\Api\V1\Blog\ForCreatePostAction::class);
Done! All files generated successfully.
Each generated file has TODO comments showing exactly what to implement.
See Real-World Examples and Quick Start Wizard for complete walkthrough.
Example: Granular Approach (Full Control)
# Scaffold bounded-context php artisan ddd:make:domain Invoicing # Generate infrastructure layer php artisan ddd:eloquent-model Invoicing:Invoice -m php artisan ddd:eloquent-model Invoicing:LineItem -m php artisan ddd:repository Invoicing:InvoiceRepository php artisan ddd:mapper Invoicing:InvoiceMapper # Generate application layer php artisan ddd:command-query Invoicing:CreateInvoiceCommand php artisan ddd:command-query Invoicing:GetInvoiceQuery --query # Generate domain layer php artisan ddd:value Invoicing:DollarAmount php artisan ddd:policy Invoicing:InvoicePolicy php artisan ddd:event Invoicing:InvoiceCreated php artisan ddd:listener Invoicing:HandleInvoiceCreated # Bind everything php artisan ddd:provider Invoicing:InvoicingServiceProvider
Credits
- Jasper Tey - Laravel DDD
- Dani Martinez - Orphail DDD
- Imran Ahmed - Ahmed Laravel DDD Maker
- Spatie - Laravel packages best practices
- All Contributors
License
The MIT License (MIT). Please see License File for more information.