jafar-albadarneh/laravel-ddd

Scaffold your Laravel services and actions in a Domain-Driven-Design architecture


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304444442e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6a616661722d616c62616461726e65682532466c61726176656c2d646464267061747465726e3d627269636b57616c6c267374796c653d7374796c655f31266465736372697074696f6e3d446f6d61696e2d44726976656e2d44657369676e2b696e2b416374696f6e732b666f722b4c61726176656c2b4d6f6e6f6c697468266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667

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).

Installation

You can install the package via composer:

composer require jafar-albadarneh/laravel-ddd

Usage

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:

Generating the Domain

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.

Generating Domain 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.

Generating Domain Actions

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.

Generating Domain DTOs

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.

Generating Domain DTOs

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.

Generating Native Laravel Resources

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.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.