In this guide, I’ll explain how to structure a Laravel project using Domain-Driven Design (DDD) principles to create modular and maintainable applications. DDD allows you to divide complex applications into different layers and modules based on the business logic and domain.
This helps in keeping the code clean, easy to understand, and scalable. By the end of this article, you’ll have a solid understanding of how to apply DDD concepts in Laravel with examples of folder structures, service layers, and aggregates.
The first step in applying DDD is to split the application into layers that reflect the domain and business logic. These typically include a Domain
and an Application
layer.
Create folders inside the app/
directory for Domain
and Application
:
app/
├── Application/
└── Domain/
In DDD, entities represent core business objects. For instance, in an e-commerce application, an Order
could be an entity.
Create an Order
entity inside the Domain
folder:
app/Domain/Entities/Order.php
namespace App\Domain\Entities;
class Order
{
private $id;
private $customer;
private $products = [];
public function __construct($id, $customer, $products)
{
$this->id = $id;
$this->customer = $customer;
$this->products = $products;
}
public function getTotal(): float
{
return array_sum(array_map(fn($product) => $product->price, $this->products));
}
}
Order
aggregate could include the Customer
and Product
entities.app/Domain/Aggregates/OrderAggregate.php
namespace App\Domain\Aggregates;
use App\Domain\Entities\Order;
use App\Domain\Entities\Customer;
class OrderAggregate
{
private $order;
private $customer;
public function __construct(Order $order, Customer $customer)
{
$this->order = $order;
$this->customer = $customer;
}
public function placeOrder(): void
{
// Place the order logic here
}
}
Repositories abstract the data layer and provide methods to interact with the data, separating the logic from the domain.
app/Domain/Repositories/OrderRepository.php
namespace App\Domain\Repositories;
use App\Domain\Entities\Order;
interface OrderRepository
{
public function save(Order $order): void;
public function findById($id): ?Order;
}
Service classes in the Application
layer handle the application logic and interact with repositories.
app/Application/Services/OrderService.php
namespace App\Application\Services;
use App\Domain\Repositories\OrderRepository;
use App\Domain\Entities\Order;
class OrderService
{
private $orderRepository;
public function __construct(OrderRepository $orderRepository)
{
$this->orderRepository = $orderRepository;
}
public function createOrder($customer, $products)
{
$order = new Order(uniqid(), $customer, $products);
$this->orderRepository->save($order);
return $order;
}
}
Finally, controllers are responsible for handling HTTP requests and passing them to the service layer.
app/Http/Controllers/OrderController.php
namespace App\Http\Controllers;
use App\Application\Services\OrderService;
use Illuminate\Http\Request;
class OrderController extends Controller
{
private $orderService;
public function __construct(OrderService $orderService)
{
$this->orderService = $orderService;
}
public function create(Request $request)
{
$customer = $request->input('customer');
$products = $request->input('products');
$order = $this->orderService->createOrder($customer, $products);
return response()->json($order);
}
}
Here’s what the final folder structure looks like:
app/
├── Application/
│ └── Services/
│ └── OrderService.php
├── Domain/
│ ├── Aggregates/
│ │ └── OrderAggregate.php
│ ├── Entities/
│ │ └── Order.php
│ └── Repositories/
│ └── OrderRepository.php
└── Http/
└── Controllers/
└── OrderController.php
You might also like:
In this example we will see laravel 8 group by query example. how to use group by in laravel 8. As you might expect...
Nov-29-2021
Hey developers! Today, I'm excited to walk you through an incredibly powerful feature in Laravel 10: searching...
Dec-27-2023
In a world where technology and human interaction blend seamlessly, artificial intelligence (AI) holds incredible potent...
Jul-17-2023
In this article, we will see how to generate a pdf file in laravel 10. Here, we will learn about laravel 10 ge...
Mar-10-2023