filippo-toso/laravel-domain-library

在Laravel中构建领域驱动应用程序

v4.0.1 2024-03-14 19:59 UTC

README

我阅读了Laravel Beyond CRUD系列,决定这就是方法。但我是个懒惰的程序员:我真的很不喜欢写很多代码。所以,我需要一个工具包来自动化尽可能多的编码。现在,它就在这里!

需求

  • PHP 7.2.5+
  • Laravel 7+

Laravel 11支持

我在这个时期有点忙。Laravel 11有一些影响此包的关键变更。在接下来的几天里,我会尽量抽出时间来发布一个新的大版本,全面支持Laravel 11。

它做了什么?

它提供

  • 多个控制台命令来创建领域驱动类
  • 特性和类以加快开发速度

命令前缀为domain:

它是如何工作的?

让我们从一个干净的Laravel安装开始。从您的首选shell

composer create-project laravel/laravel project

然后安装库

cd project
composer require filippo-toso/laravel-domain-library
composer require spatie/laravel-data spatie/laravel-model-states spatie/laravel-query-builder spatie/laravel-view-models

需要额外的Spatie包来支持数据传输对象、模型状态、查询构建器和视图模型。

现在,让我们进入有趣的部分... 首先,我们将构建领域结构

php artisan domain:setup:structure

此命令将创建一个新的src文件夹,其中包含3个命名空间AppDomainSupport。它还将重置Laravel应用程序以使用这个新的目录结构(即移动中间件和提供者、更新引导代码、引入一个新的应用程序类等)。

到这一点,您就可以开始构建您的领域驱动应用程序了。让我们为名为Invoices的示例领域创建结构

php artisan domain:make:domain Invoices

然后我们将为这个领域的管理应用程序部分做准备

php artisan domain:make:application Admin\Invoices

创建领域类

使用这个库,您可以创建以下类

  • 模型(支持自定义集合、查询构建器和订阅事件)
  • 模型状态(使用spatie/laravel-model-states)
  • 模型的订阅者和模型事件(自动关联到模型)
  • 模型的查询构建器(自动加载)
  • 模型的集合(自动加载)
  • 数据传输对象(使用spatie/data-transfer-object)
  • HTTP查询(使用spatie/laravel-query-builder)
  • 视图模型(使用spatie/laravel-view-models)
  • 操作
  • 表单请求

所有命令都包含一个--force选项来覆盖现有类。请注意,权力越大,责任越大

自定义代码生成

如果您想自定义生成的代码,可以发布占位符然后编辑它们。

php artisan vendor:publish --tag=stubs --provider="FilippoToso\Domain\Support\ServiceProvider"

创建模型

php artisan domain:make:model Invoice --domain=Invoices

创建模型状态

php artisan domain:make:states --model=Invoice --domain=Invoices --states="Paid,Pending,Overdue,Cancelled"

创建模型的订阅者

php artisan domain:make:subscriber --model=Invoice --domain=Invoices

创建模型的事件

php artisan domain:make:events --model=Invoice --domain=Invoices --events="saving,created,deleting"

创建模型的订阅者和事件

php artisan domain:make:subscriber --model=Invoice --domain=Invoices --events="saving,created,deleting"

创建模型的集合

php artisan domain:make:collection --model=Invoice --domain=Invoices

创建模型的查询构建器

php artisan domain:make:querybuilder --model=Invoice --domain=Invoices

创建数据传输对象

php artisan domain:make:dto Invoice --domain=Invoices --application=Admin\Invoices

创建HTTP查询

php artisan domain:make:query InvoiceIndex --domain=Invoices --application=Admin\Invoices --model=Invoice

创建视图模型

php artisan domain:make:viewmodel InvoiceForm --domain=Invoices --application=Admin\Invoices --model=Invoice

创建操作

php artisan domain:make:action InvoiceIndex --domain=Invoices --application=Admin\Invoices --model=Invoice

创建表单请求

php artisan domain:make:request Invoice --application=Admin\Invoices

创建异常

php artisan domain:make:exception InvalidInvoice --domain=Invoices

创建一系列类

此命令将一次性创建上述所有类

php artisan domain:make:suite 
    --domain=Invoices 
    --application=Admin\Invoices 
    --model=Invoice 
    --states="Paid,Pending,Overdue,Cancelled"
    --events="saving,created,deleting" 
    --dtos="Invoice,CreateInvoice"
    --queries=InvoiceIndex 
    --dtos="Invoice,CreateInvoice"
    --exceptions=InvalidInvoice 
    --queries=InvoiceIndex 
    --requests=Invoice 
    --viewmodels=InvoiceForm 
    --actions="CreateInvoice,PayInvoice,CancelInvoice"