This is an opinionated modified version of the Laravel framework which aims at providing a Domain-Driven Design (DDD) structure and using CycleORM instead of Eloquent.
Laravel is a popular PHP web framework known for its simplicity, elegance and the Active Record ORM - Eloquent. Despite its popularity and convenience, Eloquent often comes in the way of projects that require a Domain-Driven Design (DDD) structure. This is due to Eloquent's active record pattern, which in practice can cause the violation of DDD principles, most notably the separation of the business logic from infrastructure code.
Understanding this challenge, we've introduced CycleORM in place of Eloquent in this starter template. CycleORM offers a DataMapper pattern which is a better fit for DDD as it helps to maintain a clear boundary between your business logic and database. This approach ensures that your domain rules and business logic can be modelled according to your needs, and not around the constraints of the active record pattern.
This repository provides a modified file structure for Laravel that follows DDD principles and tries to adhere to best practices, such as those outlined in Spatie's Laravel Beyond CRUD.
Our goal is to provide a starting point for building Laravel applications using a DDD approach with CycleORM, while still leveraging the power and convenience of the Laravel framework.
π If you find this repository useful, consider giving it a βοΈ. Thank you!
This starter template inherits the following features from its upstream template wayofdev/laravel-starter-tpl:
- Added:
- Strict types declaration in all PHP files
- Style checker package for custom rule-sets to php-cs-fixer β wayofdev/php-cs-fixer-config
- Static analysis tool β PHPStan and it's extensions:
- phpstan/extension-installer β automatic installation of PHPStan extensions
- phpstan/phpstan-deprecation-rules β rules for detecting usage of deprecated classes, methods, properties, constants and traits.
- nunomaduro/larastan β PHPStan integration with Laravel
- Pest testing framework
- Github action workflows for:
- Continuous integration which includes coding standards checks, unit testing and static analysis
- Automatic pull-request labeling
- Deployer for automatic deployments to staging and production servers with support of Github Environments
- ergebnis/composer-normalize composer plugin for normalizing composer.json file
- roave/securityadvisories package to ensure that application doesn't have installed dependencies with known security vulnerabilities.
- Application dockerization using docker-compose and Makefile β use
make help
to view available commands - Git pre-commit hooks using pre-commit package
- Changed:
- Marked default Laravel classes as
final
- Modified file structure to meet DDD principles
- Changed config folder files to use default PHP multi-line comment style
- Marked default Laravel classes as
- Removed:
- Dependencies like Laravel Sanctum, Laravel Pint, and Laravel Sail.
- Broadcasting service provider and it's routes. It can be added back, if it will be required for project
- Console routes in favor of Command classes.
- Sanctum migration files
Following the inherited features, this starter template also introduces a set of additional modifications from the upstream template, wayofdev/laravel-starter-tpl:
- Added:
- CycleORM support via wayofdev/laravel-cycle-orm-adapter - This introduces the DataMapper pattern, which is more compatible with DDD principles, replacing Laravel's default Eloquent ORM which uses the Active Record pattern.
- Changed:
- Deployer script uses custom task to run Cycle ORM migrations instead of Eloquent migrations
- Removed:
- Default Laravel Eloquent migrations - As we're using CycleORM, the default Eloquent migrations are no longer necessary and have been removed to prevent conflicts.
To use this repository, you need to meet the following requirements:
- macOS Monterey+ or Linux
- Docker 20.10 or newer
- Installed dnsmasq service, running in the system. You can use ansible-role-dnsmasq to install and configure this service.
- Cloned, configured and running docker-shared-services to support system-wide DNS, routing, and TLS support via Traefik.
Warning: You should configure, set up, and run the docker-shared-services repository to ensure system-wide TLS and DNS support.
-
Clone repository:
After forking or creating generating repository from template, you can clone it to your local machine. In this example we will use
laravel-cycle-starter-tpl
repository as starting point.$ git clone git@github.com:wayofdev/laravel-cycle-starter-tpl.git
-
Generate
.env
fileGenerate .env file from .env.example file using Makefile command:
$ make env \ APP_NAME=laravel \ SHARED_SERVICES_NAMESPACE=ss \ PROJECT_SERVICES_NAMESPACE=wod \ COMPOSE_PROJECT_NAME=laravel-cycle-starter-tpl
Change generated
.env
file to match your needs, if needed.(Optional): to re-generate
.env
file, addFORCE=true
to the end of command:$ make env \ APP_NAME=laravel \ SHARED_SERVICES_NAMESPACE=ss \ PROJECT_SERVICES_NAMESPACE=wod \ COMPOSE_PROJECT_NAME=laravel-cycle-starter-tpl \ FORCE=true
-
Build, install and run. This will also generate Laravel app key:
$ make # or run commands separately $ make hooks $ make install $ make key $ make prepare $ make up
Useful resources about Laravel and DDD approach:
This repository was created in 2022 by lotyp / wayofdev.
Thank you for considering contributing to the wayofdev community! We are open to all kinds of contributions. If you want to:
- π€ Suggest a feature
- π Report an issue
- π Improve documentation
- π¨βπ» Contribute to the code