Laravel Modular Monolith: Bounded Contexts Guide | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.msaied.com) [ Home ](https://www.msaied.com) [ Projects ](https://www.msaied.com/projects) [ Articles  ](https://www.msaied.com/articles) [ Certificates ](https://www.msaied.com/certificates) [ Contact ](https://www.msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://www.msaied.com) [ Projects ](https://www.msaied.com/projects) [ Articles ](https://www.msaied.com/articles) [ Certificates ](https://www.msaied.com/certificates) [ Contact ](https://www.msaied.com#contact-section) 

  [ home ](https://www.msaied.com)    [ articles ](https://www.msaied.com/articles)    Modular Monolith in Laravel: Enforcing Bounded Contexts Without a Microservices Tax        On this page       1. [  Why a Modular Monolith? ](#why-a-modular-monolith)
2. [  Directory Layout ](#directory-layout)
3. [  Internal Contracts: The Only Crossing Point ](#internal-contracts-the-only-crossing-point)
4. [  Cross-Module Communication via Domain Events ](#cross-module-communication-via-domain-events)
5. [  Enforcing Boundaries with Deptrac ](#enforcing-boundaries-with-deptrac)
6. [  Shared Kernel: What Belongs There ](#shared-kernel-what-belongs-there)
7. [  Key Takeaways ](#key-takeaways)

  ![Modular Monolith in Laravel: Enforcing Bounded Contexts Without a Microservices Tax](https://cdn.msaied.com/316/45b214e7656a6fe4e1c42a427f394287.png)

  #laravel   #architecture   #ddd   #modular-monolith  

 Modular Monolith in Laravel: Enforcing Bounded Contexts Without a Microservices Tax 
=====================================================================================

     28 Jun 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Why a Modular Monolith?  ](#why-a-modular-monolith)
2. [  02   Directory Layout  ](#directory-layout)
3. [  03   Internal Contracts: The Only Crossing Point  ](#internal-contracts-the-only-crossing-point)
4. [  04   Cross-Module Communication via Domain Events  ](#cross-module-communication-via-domain-events)
5. [  05   Enforcing Boundaries with Deptrac  ](#enforcing-boundaries-with-deptrac)
6. [  06   Shared Kernel: What Belongs There  ](#shared-kernel-what-belongs-there)
7. [  07   Key Takeaways  ](#key-takeaways)

 Why a Modular Monolith?
-----------------------

Microservices promise isolation but deliver operational complexity. A well-structured modular monolith gives you the same conceptual boundaries — bounded contexts, explicit contracts, independent domain logic — while keeping deployment, transactions, and debugging simple.

The goal is not to split the codebase into packages. It is to enforce rules about *who may call whom* and *how* at the PHP level, so the boundaries are real rather than aspirational.

---

Directory Layout
----------------

Each bounded context lives under `src/Modules/`. The framework's `app/` directory becomes a thin bootstrap layer.

```javascript
src/
  Modules/
    Billing/
      Actions/
      Data/
      Events/
      Http/
      Models/
      Contracts/
        BillingServiceInterface.php
      BillingServiceProvider.php
    Catalog/
      ...
    Identity/
      ...
app/
  Providers/
    AppServiceProvider.php   ← registers module providers only

```

Each module registers itself via its own `ServiceProvider`. `AppServiceProvider` does nothing but call `$this->app->register(BillingServiceProvider::class)` for each module.

---

Internal Contracts: The Only Crossing Point
-------------------------------------------

Modules must never import each other's concrete classes. They communicate through interfaces declared in their own `Contracts/` namespace.

```php
// src/Modules/Billing/Contracts/BillingServiceInterface.php
namespace Modules\Billing\Contracts;

interface BillingServiceInterface
{
    public function charge(string $customerId, int $amountCents): ChargeResult;
}

```

The `Catalog` module depends on this interface, not on `Modules\Billing\Services\StripeService`.

```php
// src/Modules/Catalog/Actions/PublishProduct.php
namespace Modules\Catalog\Actions;

use Modules\Billing\Contracts\BillingServiceInterface;

final class PublishProduct
{
    public function __construct(
        private readonly BillingServiceInterface $billing
    ) {}

    public function handle(Product $product): void
    {
        $result = $this->billing->charge($product->owner_id, $product->listing_fee_cents);
        // ...
    }
}

```

Binding the concrete implementation happens inside `BillingServiceProvider`, invisible to the caller.

```php
// src/Modules/Billing/BillingServiceProvider.php
$this->app->bind(
    \Modules\Billing\Contracts\BillingServiceInterface::class,
    \Modules\Billing\Services\StripeService::class,
);

```

---

Cross-Module Communication via Domain Events
--------------------------------------------

When a module needs to *notify* rather than *query*, use Laravel's event dispatcher with typed event classes. Each module listens only to events it cares about.

```php
// src/Modules/Identity/Events/UserRegistered.php
namespace Modules\Identity\Events;

final class UserRegistered
{
    public function __construct(
        public readonly string $userId,
        public readonly string $email,
    ) {}
}

```

```php
// src/Modules/Billing/Listeners/ProvisionFreeTier.php
namespace Modules\Billing\Listeners;

use Modules\Identity\Events\UserRegistered;

final class ProvisionFreeTier
{
    public function handle(UserRegistered $event): void
    {
        // create a free subscription for $event->userId
    }
}

```

The `Identity` module fires the event and has zero knowledge of `Billing`. The listener is registered in `BillingServiceProvider`:

```php
$this->listen(UserRegistered::class, ProvisionFreeTier::class);

```

---

Enforcing Boundaries with Deptrac
---------------------------------

Conventions break under deadline pressure. Automate enforcement with [Deptrac](https://qossmic.github.io/deptrac/):

```yaml
# deptrac.yaml
layers:
  - name: Billing
    collectors:
      - type: className
        regex: ^Modules\\Billing\\
  - name: Catalog
    collectors:
      - type: className
        regex: ^Modules\\Catalog\\
ruleset:
  Catalog:
    - Billing   # Catalog may only use Billing's Contracts, enforced by regex
  Billing: []

```

Run `deptrac analyse` in CI. Any direct import of a concrete class across module boundaries fails the build.

---

Shared Kernel: What Belongs There
---------------------------------

Not everything is module-specific. A `Shared/` kernel holds:

- Base value objects (`Money`, `Email`, `Uuid`)
- Common exceptions (`DomainException`, `ValidationException`)
- Infrastructure interfaces (`ClockInterface`, `UuidGeneratorInterface`)

Modules may depend on `Shared/`. `Shared/` must never depend on any module.

---

Key Takeaways
-------------

- **Modules own their service providers** — binding, listening, and scheduling stay inside the module.
- **Contracts are the only public API** — no concrete class crosses a module boundary.
- **Events decouple producers from consumers** — the firing module never imports the listener.
- **Deptrac in CI makes boundaries real** — conventions without tooling are just comments.
- **Shared kernel stays thin** — value objects and interfaces only, no business logic.
- **Transactions still work** — because it is one process and one database connection, `DB::transaction()` spans modules freely.

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.msaied.com%2Farticles%2Fmodular-monolith-in-laravel-enforcing-bounded-contexts-without-a-microservices-tax-1&text=Modular+Monolith+in+Laravel%3A+Enforcing+Bounded+Contexts+Without+a+Microservices+Tax) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.msaied.com%2Farticles%2Fmodular-monolith-in-laravel-enforcing-bounded-contexts-without-a-microservices-tax-1) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  How is this different from just organising files into folders?        The difference is enforcement. Folder organisation is cosmetic. Bounded contexts backed by interface-only contracts and Deptrac CI rules make illegal dependencies a build failure, not a code-review suggestion. 

      Q02  Can modules share an Eloquent model, for example a User?        Avoid it. Instead, each module defines a read-only projection or a lightweight DTO for the data it needs. The Identity module owns the User model; other modules query through a contract method or listen to events. 

      Q03  Does this approach work with Filament admin panels?        Yes. Each module can register its own Filament resources inside its ServiceProvider using Filament::serving(). The panel itself lives in a dedicated Module\Admin context that imports only contracts from other modules. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://www.msaied.com/articles) 

 [ ![Filament v5 Preview: Schema Unification, Performance Shifts, and How to Prepare](https://cdn.msaied.com/340/1a05ca68637b898b676efb66f22e627f.png) filament laravel php 

### Filament v5 Preview: Schema Unification, Performance Shifts, and How to Prepare

Filament v5 is reshaping how panels, forms, and tables are composed. This deep-dive covers the confirmed archi...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 1 Jul 2026     4 min read  

  Read    

 ](https://www.msaied.com/articles/filament-v5-preview-schema-unification-performance-shifts-and-how-to-prepare) [ ![Laravel 13: New Features, Helpers, and Practical Upgrade Notes](https://cdn.msaied.com/339/58c4fa6fe9b6d25a2dac17c621b6f4c6.png) laravel laravel-13 upgrade 

### Laravel 13: New Features, Helpers, and Practical Upgrade Notes

Laravel 13 ships with async-first defaults, a leaner bootstrapping layer, and several quality-of-life helpers....

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 1 Jul 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/laravel-13-new-features-helpers-and-practical-upgrade-notes) [ ![Laravel 12: Structured Route Files, Slim Skeletons, and the New Application Bootstrapping](https://cdn.msaied.com/337/05b39d16d0f88a5fb94d0cf74049b88b.png) laravel laravel-12 upgrade 

### Laravel 12: Structured Route Files, Slim Skeletons, and the New Application Bootstrapping

Laravel 12 ships with a leaner skeleton, first-class route file organisation, and a revised application bootst...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 1 Jul 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/laravel-12-structured-route-files-slim-skeletons-and-the-new-application-bootstrapping) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [Contact](https://www.msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
