How to Build a Modular Blog in Laravel 11 (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 start, ensure you have the following:
- Laravel 11 installed
- Composer (for package management)
- PHP 8.2+ (as required by Laravel 11)
- Database (MySQL, PostgreSQL, SQLite, or any other supported database)
- Basic understanding of Laravel MVC structure
🔹 Key Features:
- Modular Architecture – Divide your app into modules (like mini Laravel apps).
- Autoloading – Each module has its own controllers, models, views, routes, and migrations.
- Service Providers – Each module can have its own service provider.
- Artisan Commands – Generate and manage modules using simple commands.
- Customization – Configure module paths, namespaces, and more.
composer create-project --prefer-dist laravel/laravel blog
🔹 Installation:
Run the following command in your Laravel project:
composer require nwidart/laravel-modules
🔹 Publish the Config File:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
Configure Autoloading
Ensure that your modules are autoloaded by adding the following to the autoload
section of your composer.json
file:
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
After updating composer.json
, regenerate the autoload files:
composer dump-autoload
Set Up Your Database in the .env
File
The .env
file in Laravel contains the environment variables used for your application, including database configuration.
Locate the .env
File
In your Laravel project root directory, you will find a file named .env
. If it doesn’t exist, create one by copying the example file:
cp .env.example .env
php artisan key:generate
Configure Database Connection
Open the .env
file and locate the database settings. Modify the values according to your database configuration:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp # Change to your database name
DB_USERNAME=root # Change to your database username
DB_PASSWORD= # Change to your database password
Variable | Description |
---|---|
DB_CONNECTION | Type of database (e.g., mysql , pgsql , sqlite ) |
DB_HOST | Database server address (usually 127.0.0.1 for local development) |
DB_PORT | Default MySQL port is 3306 |
DB_DATABASE | Name of your database |
DB_USERNAME | Your database username |
DB_PASSWORD | Your database password |
Apply the Changes
After updating .env
, run the following command to clear the cache and apply the changes:
php artisan config:clear
Create the Database
If your database doesn't exist yet, create it manually:
For MySQL:
CREATE DATABASE myapp;
Or use a database management tool like phpMyAdmin or TablePlus to create the database.
Run Migrations
Now, run Laravel migrations to set up the database tables:
php artisan migrate
🔹 Create a Module:
php artisan module:make Blog
Your composer.json
extra section includes a "merge-plugin"
directive, which is likely being used to merge module-specific composer.json
files automatically. However, this may be causing issues if nwidart/laravel-modules
isn't configured properly.
Here’s how to troubleshoot and fix your issue step by step:
Check Your Full composer.json
File
Your "extra"
section currently looks like this:
"extra": {
"laravel": {
"dont-discover": []
},
"merge-plugin": {
"include": [
"Modules/*/composer.json"
]
}
}
This setup means:
"dont-discover": []
ensures that no packages are explicitly prevented from being auto-discovered."merge-plugin"
merges thecomposer.json
files inside each module found inModules/
.
Make sure your composer.json
file also includes nwidart/laravel-modules
under require
or require-dev
:
"require": {
"php": "^8.1",
"laravel/framework": "^11.0",
"nwidart/laravel-modules": "^9.0"
}
After verifying this, run the following command:
composer update
This creates a Blog
module inside the Modules/
directory.
This structure includes:
- Config: Configuration files.
- Database: Migrations, seeders, and factories for posts.
- Http: Controllers, middleware, and request validation.
- Models: The Post model.
- Providers: Module-specific service providers.
- Resources: Views, translations, and other front-end assets.
- Routes: API and web routes for the module.
- Tests: Unit and feature tests for the module.
- post.json: Module metadata.
🔹 Module Structure:
Modules/ ├── Post/ │ ├── Config/ │ │ └── config.php │ ├── Database/ │ │ ├── Factories/ │ │ │ └── PostFactory.php │ │ ├── Migrations/ │ │ │ └── 2025_02_16_000000_create_posts_table.php │ │ ├── Seeders/ │ │ │ └── PostSeeder.php │ ├── Http/ │ │ ├── Controllers/ │ │ │ └── PostController.php │ │ ├── Middleware/ │ │ ├── Requests/ │ │ │ ├── CreatePostRequest.php │ │ │ ├── UpdatePostRequest.php │ ├── Models/ │ │ └── Post.php │ ├── Providers/ │ │ └── PostServiceProvider.php │ ├── Resources/ │ │ ├── Lang/ │ │ │ ├── en/ │ │ │ │ └── messages.php │ │ ├── Views/ │ │ │ ├── index.blade.php │ │ │ ├── create.blade.php │ │ │ ├── edit.blade.php │ ├── Routes/ │ │ ├── api.php │ │ ├── web.php │ ├── Tests/ │ │ ├── Feature/ │ │ │ └── PostTest.php │ │ ├── Unit/ │ │ │ └── PostModelTest.php │ ├── post.json │ ├── composer.json
🔹 Register Module Routes:
In Modules/Blog/Routes/web.php
:
<?php use Illuminate\Support\Facades\Route; use Modules\Blog\Http\Controllers\BlogController; // Group all blog-related routes Route::prefix('blog')->name('posts.')->group(function () {
Route::get('/', [BlogController::class, 'index'])->name('blog');
});
🔹 Enable the Module:
To enable the module if disabled, run:
php artisan module:enable Blog
Start the Laravel Development Server
Run the following command in your terminal inside your Laravel project directory:
php artisan serve
After running this command, you should see output similar to:
Starting Laravel development server: http://127.0.0.1:8000
This means your Laravel application is now running at http://127.0.0.1:8000
Access Your Application:
- Open your browser and go to http://127.0.0.1:8000
- You should see the Laravel welcome page.
Summary:
With this structure, you now have a modular post feature in your Laravel application. You can extend this by adding more functionality, like editing and deleting posts or creating a comments system.
Would you like to add any specific features, or need help with something else in this process?