Toolkit for domain driven design (DDD) in Laravel. Provides artisan commands to generate domain models, DTOs, view models, value objects, and much more.
Toolkit for domain driven design (DDD) in Laravel. Provides artisan commands to generate domain models, DTOs, view models, value objects, and much more.
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.
You can install the package via composer:
composer require tey/laravel-dddUpgrading from v2? See UPGRADING.
You may initialize the package using the ddd:install artisan command. This will publish the config file, register the domain path in your project's composer.json psr-4 autoload configuration on your behalf, and allow you to publish generator stubs for customization if needed.
php artisan ddd:installFor first-time installations, a config wizard is available to populate the ddd.php config file interactively:
php artisan ddd:config wizardFor existing installations with a config file published from a previous version, you may use the ddd:config update command to rebuild and merge it with the latest package copy:
php artisan ddd:config updateSee Configuration Utility for details about other available options.
The following additional packages are suggested (but not required) while working with this package.
The default DTO and Action stubs of this package reference classes from these packages. If this doesn't apply to your application, you may publish and customize the stubs accordingly.
In production, ensure you run php artisan ddd:optimize during the deployment process to optimize autoloading. If you already run php artisan optimize in production, this will be handled automatically.
| Laravel | LaravelDDD | |
|---|---|---|
| 9.x - 10.24.x | 0.x | 0.x README |
| 10.25.x | 1.x | |
| 11.x | 1.x | |
| 11.44.x | 2.x | |
| 12.x | 2.x | |
| 11.x - 13.x | 3.x |
See UPGRADING for more details about upgrading across different versions.
All domain generator commands use the following syntax:
# Specifying the domain as an option
php artisan ddd:{object} {name} --domain={domain}
# Specifying the domain as part of the name (short-hand syntax)
php artisan ddd:{object} {domain}:{name}
# Not specifying the domain at all, which will then
# prompt for it (with auto-completion)
php artisan ddd:{object} {name}The following generators are currently available:
| Command | Description | Usage |
|---|---|---|
ddd:model |
Generate a domain model | php artisan ddd:model Invoicing:Invoice |
Options:
--migration\|-m
--factory\|-f
--seed\|-s
--controller --resource --requests\|-crR
--policy
-mfsc
--all\|-a
--pivot\|-p
|
| ddd:factory | Generate a domain factory | php artisan ddd:factory Invoicing:InvoiceFactory |
| ddd:dto | Generate a data transfer object | php artisan ddd:dto Invoicing:LineItemPayload |
| ddd:value | Generate a value object | php artisan ddd:value Shared:DollarAmount |
| ddd:view-model | Generate a view model | php artisan ddd:view-model Invoicing:ShowInvoiceViewModel |
| ddd:action | Generate an action | php artisan ddd:action Invoicing:SendInvoiceToCustomer |
| ddd:cast | Generate a cast | php artisan ddd:cast Invoicing:MoneyCast |
| ddd:channel | Generate a channel | php artisan ddd:channel Invoicing:InvoiceChannel |
| ddd:command | Generate a command | php artisan ddd:command Invoicing:InvoiceDeliver |
| ddd:controller | Generate a controller | php artisan ddd:controller Invoicing:InvoiceController
Options: inherits options from make:controller |
| ddd:event | Generate an event | php artisan ddd:event Invoicing:PaymentWasReceived |
| ddd:exception | Generate an exception | php artisan ddd:exception Invoicing:InvoiceNotFoundException |
| ddd:job | Generate a job | php artisan ddd:job Invoicing:GenerateInvoicePdf |
| ddd:listener | Generate a listener | php artisan ddd:listener Invoicing:HandlePaymentReceived |
| ddd:mail | Generate a mail | php artisan ddd:mail Invoicing:OverduePaymentReminderEmail |
| ddd:middleware | Generate a middleware | php artisan ddd:middleware Invoicing:VerifiedCustomerMiddleware |
| ddd:migration | Generate a migration | php artisan ddd:migration Invoicing:CreateInvoicesTable |
| ddd:notification | Generate a notification | php artisan ddd:notification Invoicing:YourPaymentWasReceived |
| ddd:observer | Generate an observer | php artisan ddd:observer Invoicing:InvoiceObserver |
| ddd:policy | Generate a policy | php artisan ddd:policy Invoicing:InvoicePolicy |
| ddd:provider | Generate a provider | php artisan ddd:provider Invoicing:InvoiceServiceProvider |
| ddd:resource | Generate a resource | php artisan ddd:resource Invoicing:InvoiceResource |
| ddd:rule | Generate a rule | php artisan ddd:rule Invoicing:ValidPaymentMethod |
| ddd:request | Generate a form request | php artisan ddd:request Invoicing:StoreInvoiceRequest |
| ddd:scope | Generate a scope | php artisan ddd:scope Invoicing:ArchivedInvoicesScope |
| ddd:seeder | Generate a seeder | php artisan ddd:seeder Invoicing:InvoiceSeeder |
| ddd:class | Generate a class | php artisan ddd:class Invoicing:Support/InvoiceBuilder |
| ddd:enum | Generate an enum | php artisan ddd:enum Customer:CustomerType |
| ddd:interface | Generate an interface | php artisan ddd:interface Customer:Contracts/Invoiceable |
| ddd:trait | Generate a trait | php artisan ddd:trait Customer:Concerns/HasInvoices |
Generated objects will be placed in the appropriate domain namespace as specified by ddd.namespaces.* in the config file.
A configuration utility was introduced in 1.2 to help manage the package's configuration over time.
php artisan ddd:configOutput:
┌ Laravel-DDD Config Utility ──────────────────────────────────┐
│ › ● Run the configuration wizard │
│ ○ Rebuild and merge ddd.php with latest package copy │
│ ○ Detect domain namespace from composer.json │
│ ○ Sync composer.json from ddd.php │
│ ○ Exit │
└──────────────────────────────────────────────────────────────┘These config tasks are also invokeable directly using arguments:
# Run the configuration wizard
php artisan ddd:config wizard
# Rebuild and merge ddd.php with latest package copy
php artisan ddd:config update
# Detect domain namespace from composer.json
php artisan ddd:config detect
# Sync composer.json from ddd.php
php artisan ddd:config composer# Show a summary of current domains in the domain folder
php artisan ddd:list
# Cache domain manifests (used for autoloading)
php artisan ddd:optimize
# Clear the domain cache
php artisan ddd:clearSome objects interact with the domain layer, but are not part of the domain layer themselves. By default, these include: controller, request, middleware. You may customize the path, namespace, and which ddd:* objects belong in the application layer.
// In config/ddd.php
'application_path' => 'app/Modules',
'application_namespace' => 'App\Modules',
'application_objects' => [
'controller',
'request',
'middleware',
],The configuration above will result in the following:
ddd:model Invoicing:Invoice --controller --resource --requestsOutput:
├─ app
| └─ Modules
│ └─ Invoicing
│ ├─ Controllers
│ │ └─ InvoiceController.php
│ └─ Requests
│ ├─ StoreInvoiceRequest.php
│ └─ UpdateInvoiceRequest.php
├─ src/Domain
└─ Invoicing
└─ Models
└─ Invoice.phpOften times, additional top-level namespaces are needed to hold shared components, helpers, and things that are not domain-specific. A common example is the Infrastructure layer. You may configure these additional layers in the ddd.layers array.
// In config/ddd.php
'layers' => [
'Infrastructure' => 'src/Infrastructure',
],The configuration above will result in the following:
ddd:model Invoicing:Invoice
ddd:trait Infrastructure:Concerns/HasExpiryDateOutput:
├─ src/Domain
| └─ Invoicing
| └─ Models
| └─ Invoice.php
├─ src/Infrastructure
└─ Concerns
└─ HasExpiryDate.phpAfter defining new layers in ddd.php, make sure the corresponding namespaces are also registered in your composer.json file. You may use the ddd:config helper command to handle this for you.
# Sync composer.json with ddd.php
php artisan ddd:config composerFor any ddd:* generator command, nested objects can be specified with forward slashes.
php artisan ddd:model Invoicing:Payment/Transaction
# -> Domain\Invoicing\Models\Payment\Transaction
php artisan ddd:action Invoicing:Payment/ProcessTransaction
# -> Domain\Invoicing\Actions\Payment\ProcessTransaction
php artisan ddd:exception Invoicing:Payment/PaymentFailedException
# -> Domain\Invoicing\Exceptions\Payment\PaymentFailedExceptionThis is essential for objects without a fixed namespace such as class, interface, trait,
each of which have a blank namespace by default. In other words, these objects originate
from the root of the domain.
php artisan ddd:class Invoicing:Support/InvoiceBuilder
# -> Domain\Invoicing\Support\InvoiceBuilder
php artisan ddd:interface Invoicing:Contracts/PayableByCreditCard
# -> Domain\Invoicing\Contracts\PayableByCreditCard
php artisan ddd:interface Invoicing:Models/Concerns/HasLineItems
# -> Domain\Invoicing\Models\Concerns\HasLineItemsSubdomains can be specified with dot notation wherever a domain option is accepted.
# Domain/Reporting/Internal/ViewModels/MonthlyInvoicesReportViewModel
php artisan ddd:view-model Reporting.Internal:MonthlyInvoicesReportViewModel
# Domain/Reporting/Customer/ViewModels/MonthlyInvoicesReportViewModel
php artisan ddd:view-model Reporting.Customer:MonthlyInvoicesReportViewModel
# (supported by all commands where a domain option is accepted)If for some reason you need to generate a domain object under a namespace different to what is configured in ddd.namespaces.*,
you may do so using an absolute name starting with /. This will generate the object from the root of the domain.
…If you require advanced customization of generated object naming conventions, you may register a custom resolver using DDD::resolveObjectSchemaUsing() in your AppServiceProvider's boot method:
…The example above will result in the following:
php artisan ddd:controller Invoicing:PaymentController --api
# Controller [src/App/Api/Controllers/Invoicing/PaymentApiController.php] created successfully. This pa
No open issues yet, or sync has not completed.