In a Domain-Driven environment, you would want to keep your code as clean and organized as possible. This package allows to create a domain-driven architecture in your Laravel app. The package assumes the following code structure:
- app
- Domains
- [DomainA]
- Actions
- Events
- Http
- Listeners
- Models
- Services
By design, each domain should be treated in isolation, where all of the actions and services are scoped to the domain.
In regard to inter-domain communication, you can create a proxy service at the domain level to bridge the communication gap between the domains. Internally you're free to either:
- Inject and class services directly
- Follow an internal pub/sub mechanism, where you publish events from (Domain X) and listen to them in other domains [Domain Y, Domain Z ..etc).
You can install the package via composer:
composer require jafar-albadarneh/laravel-ddd
The package is equipped with a command line tool that allows you to create a domain-driven architecture in your Laravel app. These command line tools include:
After you identify the domain, you can generate it by running the following command:
php artisan make:domain [domain-name]
The command accepts the following options:
[domain-name]
: The name of the domain you want to create.--with-samples=1
: If you want to generate the domain with sample actions and services.
After creating the domain, you can generate the services by running the following command:
php artisan make:service domain=[domain-name]
The command accepts the following options:
domain=[domain-name]
: The name of the domain you want to generate the services for.--name=[service-name]
: If you want to generate a service with a custom name.
After creating the domain, you can generate the actions by running the following command:
php artisan make:action domain=[domain-name] --name=[action-name]
The command accepts the following options:
domain=[domain-name]
: The name of the domain you want to generate the actions for.--name=[action-name]
: The name of action class within the domain.
After creating the domain, settle with your services, you can generate DTOs by running the following command:
php artisan make:dto domain=[domain-name] --name=[dto-name]
The command accepts the following options:
domain=[domain-name]
: The name of the domain you want to generate the DTO for.--name=[dto-name]
: The name of DTO class within the domain.
After creating the domain, you can generate DTOs to support data streams among your services and actions by running the following command:
php artisan make:dto domain=[domain-name] --name=[dto-name]
The command accepts the following options:
domain=[domain-name]
: The name of the domain you want to generate the actions for.--name=[action-name]
: The name of DTO class within the domain.
You won't be getting the full potential of the package if you can't associate native laravel resources (Controllers, Requests, Resources, Middlewares) with your domain. There are two ways to achieve this:
1- Laravel artisan commands already support passing a namespace to any command. So instead of placing all the resources within the App\Http
namespace, you can prefix your resource with the FULL domain namespace.
For example, if you want to create a controller for the Authentication
domain, you can run the following command:
php artisan make:controller \\App\\Domains\\Authentication\\Http\\Controllers\\LoginController
2- [TODO] The package overrides Laravel artisan commands to support passing a domain name to the command. So instead of passing the full namespace of the domain, you can pass a --domain=[domain-name]
parameter to your command.
Note: This feature is not yet implemented.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.