Laravel Modules Explained for Developers

Laravel Modules Explained for Developers

Modern applications often grow very large. When a Laravel project becomes big, managing everything inside a single app/ folder can become difficult. This is where Laravel Modules help.

Laravel Modules allow developers to organize an application into independent modules, where each module contains its own controllers, models, routes, migrations, and views.

In this guide, you will learn:

  • What Laravel Modules are

  • Why developers use them

  • How modules work internally

  • How to install module support

  • How to create and manage modules step-by-step

1. What Are Laravel Modules?

A Laravel Module is a self-contained feature package inside a Laravel application.

Each module contains everything related to one feature.

Example structure:

Modules
├── User
│ ├── Http
│ │ └── Controllers
│ ├── Models
│ ├── Routes
│ ├── Database
│ │ └── Migrations
│ └── Views

├── Product
│ ├── Http
│ ├── Models
│ ├── Routes
│ └── Views

Each module behaves like a mini Laravel application.

2. Why Use Laravel Modules?

Developers use modules to solve large application complexity.

Benefits

1. Better Code Organization

Instead of:

app/Http/Controllers
app/Models
app/Services

You get:

Modules/User/
Modules/Product/
Modules/Order/

Each feature is isolated.

2. Easier Maintenance

If you want to update User Management, you only work inside:

Modules/User

No need to search across the entire project.

3. Team Collaboration

Multiple developers can work on different modules:

DeveloperModule
Dev AUser Module
Dev BProduct Module
Dev COrder Module

No conflicts.

4. Reusable Features

Modules can be moved between projects.

Example reusable modules:

  • Authentication Module

  • Blog Module

  • Payment Module

  • Notification Module

3. How Laravel Modules Work

Laravel does not include modules by default.

But developers commonly use the package:

nwidart/laravel-modules

This package adds:

  • Module generator

  • Module routing

  • Module migrations

  • Module views

  • Module service providers

4. Install Laravel Modules Package

Inside your Laravel project run:

composer require nwidart/laravel-modules

Publish the configuration file:

php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"

Now you will see:

config/modules.php

5. Create Your First Module

Run:

php artisan module:make Blog

Laravel will generate:

Modules/
└── Blog
├── Config
├── Database
│ ├── Factories
│ ├── Migrations
│ └── Seeders
├── Entities
├── Http
│ ├── Controllers
│ └── Middleware
├── Providers
├── Resources
│ └── views
├── Routes
│ ├── api.php
│ └── web.php
└── module.json

This module is fully functional.

6. Module Folder Structure Explained

Config

Modules/Blog/Config

Stores module configuration.

Example:

blog.php

Controllers

Modules/Blog/Http/Controllers

Example:

BlogController.php

Models (Entities)

Modules/Blog/Entities

Example:

Post.php

Routes

Modules/Blog/Routes/web.php

Example:

Route::get('/blog', function () {
return "Blog Module Working";
});

Views

Modules/Blog/Resources/views

Example:

index.blade.php

Migrations

Modules/Blog/Database/Migrations

Example migration:

Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});

Run migrations:

php artisan module:migrate

7. Create Controller Inside Module

Command:

php artisan module:make-controller BlogController Blog

Controller location:

Modules/Blog/Http/Controllers/BlogController.php

Example:

namespace Modules\Blog\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class BlogController extends Controller
{
public function index()
{
return view('blog::index');
}
}

8. Create Model Inside Module

Command:

php artisan module:make-model Post Blog

Location:

Modules/Blog/Entities/Post.php

Example:

namespace Modules\Blog\Entities;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable = ['title','content'];
}

9. Module Routes Example

Open:

Modules/Blog/Routes/web.php

Example:

use Illuminate\Support\Facades\Route;
use Modules\Blog\Http\Controllers\BlogController;

Route::prefix('blog')->group(function () {

Route::get('/', [BlogController::class, 'index']);

});

Now visit:

http://localhost:8000/blog

10. Module Views

Create:

Modules/Blog/Resources/views/index.blade.php

Example:

<h1>Blog Module</h1>
<p>This page is loaded from a Laravel module.</p>

Load the view:

return view('blog::index');

11. List All Modules

Command:

php artisan module:list

Example output:

+-------+---------+--------+
| Name | Status | Order |
+-------+---------+--------+
| Blog | Enabled | 0 |
| User | Enabled | 0 |
| Order | Enabled | 0 |
+-------+---------+--------+

12. Enable or Disable Modules

Disable module:

php artisan module:disable Blog

Enable module:

php artisan module:enable Blog

This is useful when deploying large systems.

13. Real World Example

Large systems commonly use modules:

Example SaaS Platform

Modules
├── Auth
├── User
├── Billing
├── Subscription
├── Reports
├── Notifications
└── Dashboard

Each feature is independent.

14. When Should You Use Laravel Modules?

Use modules when:

✔ Building large applications
✔ Working in large development teams
✔ Creating reusable features
✔ Building SaaS platforms

Avoid modules when:

✖ Small projects
✖ Simple websites

15. Laravel Modules vs Normal Laravel Structure

FeatureStandard LaravelLaravel Modules
StructureFeature mixedFeature separated
ScalabilityMediumVery High
Team CollaborationHarderEasier
ReusabilityLowHigh

Conclusion

Laravel Modules are a powerful way to structure large Laravel applications. By dividing features into independent modules, developers can build clean, scalable, and maintainable systems.

Modules are widely used in:

  • Enterprise applications

  • SaaS platforms

  • Large Laravel systems

  • Multi-team development projects

If your Laravel project is growing quickly, adopting a modular architecture can significantly improve development speed and maintainability.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close