Building a Modular Monolith in Laravel: Structure and Practices
Structure a Laravel app as a modular monolith: module folders, service providers, routes and migrations per module, public APIs, events and deptrac enforceme...
Published November 27, 2026
Laravel's default structure groups code by technical type: all controllers in one folder, all models in another. That is convenient for small apps but hides business boundaries as the app grows. You can keep everything you like about Laravel while organising code into business modules, each with its own controllers, models, routes, migrations and service provider. This guide shows a practical structure.
The concept
Each module lives under app/Modules/<Name> (or a top-level modules/ directory) and registers itself through its own service provider. The provider loads the module's routes, migrations, views, config and event listeners, so adding or removing a module is a single line in bootstrap/providers.php. Composer's PSR-4 autoloading maps the namespace to the folder.
Inside a module, a small Api (or Contracts) namespace contains the public facade and DTOs other modules may use. Everything else, including Eloquent models, is internal. Cross-module side effects use Laravel events; listeners that do slow work implement ShouldQueue. Architecture rules are enforced with deptrac or PHPArkitect in CI.
Folder structure
A structure that scales from three modules to thirty.
app/
Modules/
Catalog/
Api/
CatalogApi.php ← public facade
ProductDto.php
Events/ProductPublished.php ← public events
Http/Controllers/
Models/Product.php ← internal (Eloquent)
Services/
Database/Migrations/
routes/api.php
CatalogServiceProvider.php
Checkout/
Api/ Events/ Http/ Models/ Listeners/ Database/ routes/
CheckoutServiceProvider.php
Billing/ ...
Shared/ ← Money, ids, base classes (keep tiny)How a module is wired
Module interactions
Mapping Laravel features to modules
| Concern | Laravel tool | Module practice |
|---|---|---|
| Routing | Route files | Each provider loads its own routes/api.php with a prefix |
| Migrations | loadMigrationsFrom() | Per-module migration folders; table prefix per module |
| Events | Event / Listener / ShouldQueue | Public events in Events/, listeners in the subscribing module |
| Config | mergeConfigFrom() | config/<module>.php inside the module |
| Tests | PHPUnit / Pest | tests/Modules/<Name> mirroring the module |
| Boundaries | deptrac / PHPArkitect | CI rule: only Api and Events are importable across modules |
Practical examples
Module service provider
Everything the module needs is registered here.
namespace App\Modules\Catalog;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class CatalogServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(Api\CatalogApi::class);
}
public function boot(): void
{
Route::middleware('api')->prefix('api/catalog')->group(__DIR__.'/routes/api.php');
$this->loadMigrationsFrom(__DIR__.'/Database/Migrations');
}
}
// bootstrap/providers.php: App\Modules\Catalog\CatalogServiceProvider::class,Public facade returning DTOs
Callers never receive Eloquent models from another module.
namespace App\Modules\Catalog\Api;
use App\Modules\Catalog\Models\Product;
final class CatalogApi
{
public function productForCheckout(string $sku): ?ProductDto
{
$p = Product::query()->where('sku', $sku)->where('published', true)->first();
return $p ? new ProductDto($p->id, $p->sku, $p->title, $p->price_paise, $p->stock > 0) : null;
}
}Cross-module event and queued listener
Billing reacts to checkout without checkout depending on billing.
// app/Modules/Checkout/Events/OrderPlaced.php
final class OrderPlaced { use Dispatchable; public function __construct(public readonly int $orderId, public readonly int $totalPaise) {} }
// app/Modules/Billing/Listeners/CreateInvoice.php
final class CreateInvoice implements ShouldQueue
{
public function handle(OrderPlaced $event): void { /* create invoice from billing's own data */ }
}
// registered in BillingServiceProvider: Event::listen(OrderPlaced::class, CreateInvoice::class);Common mistakes
- Sharing Eloquent models across modules, which leaks relationships and lazy loading.
- Eloquent relationships that cross module boundaries (Order belongsTo Product from another module).
- One giant app/Models folder kept "for convenience" alongside modules.
- Forgetting the queue connection: with QUEUE_CONNECTION=sync, queued listeners run inline.
- No deptrac rule, so boundaries quietly erode.
Key takeaways
- Organise Laravel code into business modules, each with its own service provider.
- Expose a small Api facade with DTOs; keep Eloquent models internal.
- Use Laravel events (queued where slow) for cross-module reactions.
- Enforce boundaries with deptrac or PHPArkitect in CI.
FAQ
Should I use a package like nwidart/laravel-modules?
It provides generators and conventions that some teams like. A plain folder structure with service providers works just as well and has fewer dependencies.
Can modules share the same database connection?
Yes. Use table prefixes or separate schemas and enforce that each module only touches its own tables.
Where do shared things like the User model go?
Give users to an Accounts (or Identity) module that exposes what others need via its API. Keep the Shared folder tiny.