How to Build a Modular Blog in Laravel

How to Build a Modular Blog in Laravel

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.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install the Laravel Modules Package

composer require nwidart/laravel-modules

Step 3: Publish Configuration File

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

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:

"autoload": { "psr-4": { "App\\": "app/", "Modules\\": "Modules/" } }

Then regenerate the autoload files:

composer dump-autoload

Step 5: Set Up the Database

Create .env If it doesn't exist:

cp .env.example .env php artisan key:generate

Update .env For your DB:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=myapp DB_USERNAME=root DB_PASSWORD=

Clear and cache config:

php artisan config:clear

Step 6: Create Your Database

Manually via terminal or tool:

CREATE DATABASE myapp;

Step 7: Run Migrations

php artisan migrate

Step 8: Create a Module

php artisan module:make Blog

This generates the Modules/Blog structure like:

Modules/ └── Blog/ ├── Config/ ├── Database/ │ └── Migrations/ ├── Http/ │ └── Controllers/ ├── Models/ ├── Providers/ ├── Resources/ │ └── Views/ ├── Routes/ │ ├── web.php ├── composer.json └── post.json

Step 9: Register Module Routes

Open Modules/Blog/Routes/web.php:

<?php use Illuminate\Support\Facades\Route; use Modules\Blog\Http\Controllers\BlogController; Route::prefix('blog')->name('posts.')->group(function () { Route::get('/', [BlogController::class, 'index'])->name('index'); });

Step 10: Create a Basic Controller

Run:

php artisan module:make-controller BlogController Blog

Then update Modules/Blog/Http/Controllers/BlogController.php:

namespace Modules\Blog\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Routing\Controller; class BlogController extends Controller { public function index() { return view('blog::index'); } }

Step 11: Create a View

Create Modules/Blog/Resources/Views/index.blade.php:

<!DOCTYPE html> <html> <head> <title>Modular Blog</title> </head> <body> <h1>Welcome to the Blog Module!</h1> </body> </html>

Step 12: Enable the Module (If Disabled)

php artisan module:enable Blog

Step 13: Optional – Composer Merge Plugin

If you're using per-module composer.json, ensure this in the main composer.json:

"extra": { "merge-plugin": { "include": [ "Modules/*/composer.json" ] } }

Then run:

composer update

Step 14: Start the Laravel Development Server

php artisan serve

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.

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