This is an opinionated modified version of the Laravel framework which aims at providing a Domain-Driven Design (DDD) structure.
Laravel is a popular PHP web framework that provides an easy and efficient way to build web applications. However, the default structure of Laravel, coupled with Eloquent's active record pattern, may not always fit the needs of a project that requires a Domain-Driven Design (DDD) architecture. Eloquent's active record pattern breaks DDD principles and make it difficult to separate your business logic from your infrastructure code.
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. The goal is to provide a starting point for building Laravel applications using a DDD approach, 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 includes several added, changed, and removed features:
-
Added:
- Strict types declaration in all PHP files
- Application dockerization using docker-compose and Makefile β use
make help
to view available commands - spatie/laravel-route-attributes package for route definition using PHP8 style attributes
-
Changed:
- Marked default Laravel classes as
final
- Modified file structure to meet DDD principles (as close as possible)
- 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 its routes. It can be added back if required for the project
- Console routes in favor of Command classes
- Sanctum migration files
- Added:
- Style checker package for custom rule-sets to php-cs-fixer β wayofdev/php-cs-fixer-config
- Phive support for managing PHP tools
- ergebnis/composer-normalize composer plugin for normalizing composer.json file
- maglnet/ComposerRequireChecker to check whether a specific composer package uses imported symbols that aren't part of its direct composer dependencies
- Application dockerization using docker-compose and Makefile β use
make help
to view available commands- docker-php-dev as PHP development environment with XDebug support out of the box
- Added:
- Configured PHPUnit for unit testing.
- Integrated Pest PHP for elegant console support and architecture testing.
- Infection for mutation testing to ensure quality.
- Mutation test reports are uploaded Stryker Mutator Dashboard.
- Added:
- 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.
- larastan/larastan for Laravel-specific static analysis.
- Psalm static analysis tool and it's extensions:
- psalm/plugin-laravel β Psalm plugin for Laravel
- psalm/plugin-phpunit β Psalm plugin for PHPUnit
- PHP Rector for automated code upgrades and refactoring, maintaining modern code standards.
- Static analysis tool β PHPStan and it's extensions:
- Added:
- GitHub action workflows for:
- Continuous integration which includes coding standards checks, unit testing and static analysis
- Automatic pull-request labeling
- Git pre-commit hooks using pre-commit package
- GitHub action workflows for:
- Added:
- Deployer for automatic deployments to staging and production servers with support of GitHub Environments
- For more information see Deployments section
To use this repository, you need to meet the following requirements:
- Operating System: macOS Monterey+, Linux, or Windows with WSL2.
- Docker: Version 26.0.0 or newer. Installation guides:
- Cloned, configured and running docker-shared-services to support system-wide DNS, routing, and TLS support via Traefik.
Note
You should configure, set up, and run the docker-shared-services repository to ensure system-wide TLS and DNS support.
Check full instructions in docker-shared-services repository.
-
Create shared project directory:
mkdir -p ~/projects/infra && cd ~/projects/infra
-
Clone
docker-shared-services
repository:git clone \ [email protected]:wayofdev/docker-shared-services.git \ ~/projects/infra/docker-shared-services && \ cd ~/projects/infra/docker-shared-services
-
Create
.env
file:make env
-
Install root certificate and generate default project certs:
make cert-install
-
Run shared services:
make up
-
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-starter-tpl
repository as starting point.git clone \ [email protected]:wayofdev/laravel-starter-tpl.git \ ~/projects/laravel-starter-tpl && \ cd ~/projects/laravel-starter-tpl
-
Generate
.env
fileGenerate
.env
file from.env.example
file usingMakefile
command:$ make env \ APP_NAME=laravel \ SHARED_SERVICES_NAMESPACE=ss \ COMPOSE_PROJECT_NAME=laravel-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 \ COMPOSE_PROJECT_NAME=laravel-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
-
Open your browser and navigate to
https://laravel-starter-tpl.docker
to see Laravel welcome page.
The project architecture of wayofdev/laravel-starter-tpl
follows a structured approach with distinct layers:
- Domain: Contains core business logic and entities.
- Bridge: Connects the domain layer with external systems or services.
- Infrastructure: Handles interactions with external systems, such as databases, APIs, or file systems.
- Support: Provides general-purpose helper classes that assist various parts of the application.
- DatabaseSeeders: Handles database seeding logic.
- DatabaseFactories: Manages database factory definitions.
- Tests: Contains test cases for various layers of the application.
Each layer has defined dependencies, ensuring a clear separation of concerns and facilitating maintainability and scalability.
For more information check deptrac.yaml located in repository app
folder.
This repository utilizes GitHub Actions for continuous deployment to both staging and production servers. Below is a description.
- Push to
develop
branch: Initiates the deployment process to the staging server. Merge PR againstdevelop
branch, to triggerdeploy-staging.yml
- GitHub Actions: Executes the
deploy-staging.yml
workflow. - Staging Server: Receives deployment instructions and acknowledges the deployment.
- Merge of
develop
branch intomaster
: Triggers the deployment process to the production server on push event. - GitHub Actions: Runs the
create-release.yml
workflow. - Release Event:
deploy-release.yml
waits for the eventrelease: released
. - GitHub Actions: Initiates the
deploy-release.yml
workflow. - Production Server: Receives deployment instructions and acknowledges the deployment.
-
Deployment Successful: Both staging and production servers send a success message back to GitHub Actions. Additionally, a success message is sent to the Slack server.
-
Deployment Failed: If an error occurs, a failure message is sent back to GitHub Actions. Additionally, a failure message is sent to the Slack server.
Useful resources about Laravel and DDD approach:
This project has a security policy.
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
You are more than welcome. Before contributing, kindly check our contribution guidelines.
- Twitter: Follow our organization @wayofdev and the author @wlotyp.
- Discord: Join our community on Discord.