How to integrate module system in laravel 12
Building a Modular System in Laravel 12
When we write a small application in Laravel, Laravel's default folder structure does it all.
But as the project progresses rapidly, having all the view routes and controllers in one place can become a hassle.
Now this is where a modular based approach comes in handy. Your application is broken down into self-contained modules, each part of which lives in a mini Laravel app, which makes your code base more maintainable and error-free.
What is a modular system?
A modular system is a way where you break your project into parts called modules. Each module contains everything it needs, such as controllers, routes, models, views, migrations, and even configurations.
For example:
- A blog module might contain posts, comments, blog categories, etc.
- A system user module might contain their profile, login, registration, etc.
- An admin module might contain their dashboard, reports, etc.
The goal of making a project modular is to develop new features without touching old features. Each developer is free to work in their own module. If a problem occurs in one module, it will not affect any other module.
Why Use Modules in Laravel 12?
- Better organization – Every feature has its own space.
- Easier collaboration – Multiple developers can work on separate modules.
- Reusable features – Modules can often be reused across projects.
- Scalability – Large applications remain manageable.
- Independent testing – Each module can be tested in isolation.
Setting Up Laravel 12
Before go into modular development, start with a fresh Laravel installation:
composer create-project laravel/laravel modular-app "12.*"
Configure your .env file for app name, database , and tour site URL. Once the app is running , you are ready to add modular based work.
selecting a Module Approach
There are two main ways to add modules in Laravel:
- Manual Structure – You create a Modules / folders in app/ and organize code manually.
- Using a Package – A popular package like nwidart/laravel-modules it creates every thing automatically
While the manual way works for smaller projects, the nWidart package is a best solution that gives you ready-made commands and a clean structure. We’ll use nwidart/laravel-modules package in this guide.
Installing nWidart/laravel-modules
Install the nWidart/laravel-modules package with Composer:
composer require nwidart/laravel-modulesNex t, publish the configuration file:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
You’ll now have you have to new config/modules.php file where you can adjust settings such as the modules path.
Now Create Your First Module
Let’s create a first Blog module:
php artisan module:make Blog
This generates a folder Modules/ Blog / with the following structure:
Config/
Database/
Http/Controllers/
Models/
Resources/views/
Routes/web.php
Notice how it looks Laravel itself—it just like have a mini-app inside your project.
Routing Inside Modules
Each modules has its own Routes / web.php file. Open Modules/Blog/Routes/web.php and add:
<?php
use Illuminate\Support\Facades\Route;
use Modules\Blog\Http\Controllers\BlogController;
Route::get('/blog', [BlogController::class, 'index']);
Now the /blog route is active and handled by your Blog module.
Controllers and Views
Now generate a controller for the Blog module:
php artisan module:make-controller BlogController Blog
Inside BlogController , add a simple method:
public function index()
{
return view('blog::index');
}
Create a Blade file at Modules/Blog/Resources/views/index.blade.php:
<h1>Welcome to the Blog Module</h1>
Visit /blog in the browser—you’ll see your module in action.
Models and Migrations
Now you can see Modules can also have their own models and migrations. To create a Post model inside Blog:
php artisan module:make-model Post Blog
This also creates a migration under Modules/Blog/Database/Migrations. For example:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
Run all these module migrations with:
php artisan migrate
Now your module has its own database structure.
Service Providers
Each module has its own Providers folder where you can register services, bindings, or custom logic. The service provider is automatically loaded, so you can bind repositories, register middleware, or publish config files directly from a module.
Building APIs in Modules
Just like web routes, modules also support Routes/api.php. For example:
Route::get('/posts', [PostController::class, 'index']);
This way, your Blog module now serve both web pages and API endpoints without interfering with other modules.
Communication Between Modules
Sometimes, one module needs to talk to another—for example, Blog might need User info. The best practice is to use service classes or repositories so modules remain loosely coupled. Avoid directly importing one module’s controller into another.
Testing Modules
Modules come with their own test folders. You can write unit tests and feature tests just like in a normal Laravel app, but scoped to that specific module.
Advanced Features
- Module Caching: Speeds up route loading and config discovery.
- Asset Publishing: Publish CSS, JS, or images from a module into public/.
- Dynamic Modules: Enable or disable modules on demand.
Best Practices
- Create modules for features, not for every little thing.
- Keep modules self-contained.
- Document each module for easier onboarding.
- Avoid tight coupling—use contracts and interfaces where possible.
Example Project
To see the power of modular design, try building a project with:
- User Module: Authentication, profiles.
- Blog Module: Posts, categories, comments.
- Admin Module: Dashboard with CRUD operations.
- Each module lives independently but contributes to the full application.
Conclusion
By integrating a modular system in Laravel 12, you turn your app into a collection of well-structured, reusable, and maintainable features. Using the nWidart/laravel-modules package saves time and enforces consistency, so your team can focus on building features instead of managing messy code.
With modules, Laravel scales gracefully from small prototypes to enterprise-level systems—one feature at a time.