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 article, we will see skype screen sharing is not working in ubuntu 22.04. When you install skype in ubuntu...
Feb-15-2023
Hello, laravel web developers! In this article, we'll see how to send real-time notifications in laravel 11 usi...
Sep-09-2024
Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...
Jun-14-2021
Hello, laravel web developers! In this article, we'll see how to Socialite login with Slack in laravel 11. In l...
Aug-16-2024