Getting Started — Introduction to Laravel × DDD × Clean Architecture
This book is continuously improved. If you notice anything or have suggestions, feel free to leave a comment.
What you will learn
This book is aimed at developers who have SPA development experience with Vue/React and want to learn backend API development. It explains the ideas behind domain-driven design (DDD) and clean architecture with Laravel, step by step.
This book covers Laravel from the basics. You learn the fundamentals of Laravel in Chapter 2, "Basics of Laravel API development," before moving on to the world of DDD.
By the time you finish this book, you will be able to:
- Build an API backend with Laravel
- Understand and implement the core DDD concepts (entities, value objects, aggregates, repositories)
- Apply DDD ideas to a Laravel project
- Understand the difference between Eloquent models and domain entities, and use each appropriately
- Properly design persistence across multiple tables and transaction management
The architecture assumed in this book
This book assumes a SPA frontend + API backend architecture.
The audience and prerequisites for this book are summarized on the guide's top page. Please review them once before reading on.
This book uses PHP 8.2+ syntax (readonly, enum, constructor promotion, and so on). Chapter 2 also explains this syntax, so you will be fine even if you only have experience with PHP 7.x.
How this book is structured
This book is organized into five parts. The theme and chapters of each part are summarized in the structure diagram and learning path on the guide's top page.
The example used in this book
This book uses the order system of an e-commerce site as its running example.
Through this example, you learn how each DDD concept is translated into implementation.
About the sample code
The sample code in this book assumes the following environment:
- PHP 8.2+
- Laravel 11+
The code is sometimes simplified for explanation. In a real project, add error handling, validation, and so on.
Overview of DDD and clean architecture
What is DDD?
DDD (Domain-Driven Design) is a software design approach centered on business logic, proposed by Eric Evans.
A "domain" refers to the business area or problem space that software tries to solve. For an e-commerce site, for example, "orders," "inventory management," and "payments" all fall under the domain.
The core ideas of DDD are as follows:
- Put business logic at the center of the code: design with business rules as the top priority, not the database or the framework
- Dialogue with domain experts: people with business knowledge and engineers collaborate and design with a shared language
- Express the business in code: make "what this system does" readable from the code
Traditional development tends to start from "how do we store this (DB design)," but DDD starts by thinking about "what does this system do (business logic)" first. This lets you build a system that is resilient to changes in business requirements.
What is clean architecture?
Clean architecture is an architectural pattern that isolates business logic from external dependencies, proposed by Robert C. Martin (Uncle Bob).
Key principles of clean architecture:
- Direction of dependencies: the outer layers (UI, DB) depend on the inner layers (business logic), but the inner layers know nothing about the outer ones
- Independence of business logic: even if the framework or database changes, the business logic is unaffected
- Testability: because external dependencies can be detached, unit testing becomes easy
In this book, we implement DDD ideas with clean architecture.
Reference resources
As you work through this book, also refer to the following resources.
Official Laravel documentation
- Laravel Documentation - the official Laravel documentation
- Laravel API Reference - the API reference
The original DDD texts
-
Eric Evans, Domain-Driven Design: Tackling Complexity in the Heart of Software (commonly called the "DDD book," or "blue book")
- The original text by the creator of DDD. Recommended for those who want to study deeply.
-
Vaughn Vernon, Implementing Domain-Driven Design (commonly called the "IDDD book," or "red book")
- A DDD book that emphasizes implementation, with plenty of code examples.
Clean architecture
- Robert C. Martin, Clean Architecture
- A book by the proponent of clean architecture.
The books above focus mainly on theory, whereas this book specializes in implementation with Laravel. Reading it together with the theory books gives you a deeper understanding.
Now, let's step into the world of DDD.