How to Build a Modular Blog in Laravel (Easy Steps!)
Laravel Modules is a package that helps developers organize their applications into separate, self-contained modules. It is commonly used in large applications to keep code structured and maintainable.
This package is developed by nWidart and allows you to separate different parts of your Laravel project (e.g., authentication, orders, users, etc.) into independent modules, each containing its own controllers, models, views, routes, migrations, and more.
Requirements
Before you begin:
-
Laravel 11 installed
-
Composer (dependency management)
-
PHP 8.2 or higher
-
A supported database (MySQL, PostgreSQL, SQLite, etc.)
-
Basic understanding of Laravel MVC
Step 1: Install a Fresh Laravel Project
Skip this step if you already have a Laravel 11 project.
Step 2: Install the Laravel Modules Package
Step 3: Publish Configuration File
This publishes the config to config/modules.php
.
Step 4: Configure Autoloading in composer.json
Open composer.json
, and update the autoload.psr-4
section:
Then regenerate the autoload files:
Step 5: Set Up the Database
Create .env
If it doesn't exist:
Update .env
For your DB:
Clear and cache config:
Step 6: Create Your Database
Manually via terminal or tool:
Step 7: Run Migrations
Step 8: Create a Module
This generates the Modules/Blog
structure like:
Step 9: Register Module Routes
Open Modules/Blog/Routes/web.php
:
Step 10: Create a Basic Controller
Run:
Then update Modules/Blog/Http/Controllers/BlogController.php
:
Step 11: Create a View
Create Modules/Blog/Resources/Views/index.blade.php
:
Step 12: Enable the Module (If Disabled)
Step 13: Optional – Composer Merge Plugin
If you're using per-module composer.json
, ensure this in the main composer.json
:
Then run:
Step 14: Start the Laravel Development Server
Open http://127.0.0.1:8000/blog in your browser.
You should see: "Welcome to the Blog Module!"
Summary
You now have:
-
Laravel 11 modular architecture
-
One module (
Blog
) with its own routes, controller, and view -
Ability to extend with models, migrations, seeders, etc.