This guide walks you through creating your first module, understanding its structure, and building a simple feature using Laravel Modular.
Create Your First Module
Let’s create a “blog” module to demonstrate the workflow:
Generate the module
Run the make:module command with your desired module name: php artisan make:module blog
You’ll see output showing the files being created: Creating initial module files
- Wrote to composer.json
- Wrote to src/Providers/BlogServiceProvider.php
- Wrote to routes/blog-routes.php
- Wrote to resources/views/.gitkeep
- Wrote to database/factories/.gitkeep
- Wrote to database/migrations/.gitkeep
- Wrote to database/seeders/.gitkeep
- Wrote to tests/.gitkeep
Updating application composer.json file
- Adding path repository for app-modules/*
- Adding require statement for modules/blog:*
- Wrote to /path/to/composer.json
Please run composer update modules/blog
If you’re creating your first module and haven’t customized the namespace, you’ll be prompted to consider publishing the config first. You can proceed with the default or cancel to customize.
Update Composer dependencies
Laravel Modular automatically adds your module to your application’s composer.json. Now install it: composer update modules/blog
This registers the module’s autoloader and makes it available to your application.
Verify the module was created
List all modules to confirm: You should see your blog module listed: +--------+-------------------+
| Module | Path |
+--------+-------------------+
| blog | app-modules/blog |
+--------+-------------------+
Understanding Module Structure
Let’s explore what was created:
app-modules/blog/
├── composer.json # Module's Composer configuration
├── src/
│ └── Providers/
│ └── BlogServiceProvider.php # Service provider for the module
├── routes/
│ └── blog-routes.php # Module-specific routes
├── resources/
│ └── views/ # Blade views (namespaced as 'blog::')
├── database/
│ ├── factories/ # Model factories
│ ├── migrations/ # Database migrations
│ └── seeders/ # Database seeders
└── tests/ # Module tests
Module composer.json
Each module has its own composer.json that defines its namespace and dependencies:
app-modules/blog/composer.json
{
"name" : "modules/blog" ,
"description" : "" ,
"type" : "library" ,
"version" : "1.0" ,
"license" : "proprietary" ,
"require" : {},
"autoload" : {
"psr-4" : {
"Modules \\ Blog \\ " : "src/" ,
"Modules \\ Blog \\ Tests \\ " : "tests/" ,
"Modules \\ Blog \\ Database \\ Factories \\ " : "database/factories/" ,
"Modules \\ Blog \\ Database \\ Seeders \\ " : "database/seeders/"
}
},
"minimum-stability" : "stable" ,
"extra" : {
"laravel" : {
"providers" : [
"Modules \\ Blog \\ Providers \\ BlogServiceProvider"
]
}
}
}
The extra.laravel.providers section enables Laravel’s package discovery, so your module is automatically loaded.
Service Provider
Every module includes a service provider for registering services:
app-modules/blog/src/Providers/BlogServiceProvider.php
<? php
namespace Modules\Blog\Providers ;
use Illuminate\Support\ ServiceProvider ;
class BlogServiceProvider extends ServiceProvider
{
public function register () : void
{
// Register module services, bindings, or singletons
}
public function boot () : void
{
// Bootstrap module resources, views, translations, etc.
}
}
Building a Feature
Let’s build a simple blog post feature using Laravel’s make: commands with the --module option.
Create a model
Generate a Post model in the blog module: php artisan make:model Post --module=blog -m
The -m flag creates a migration alongside the model. This creates:
app-modules/blog/src/Models/Post.php
app-modules/blog/database/migrations/YYYY_MM_DD_HHMMSS_create_posts_table.php
Create a controller
Generate a resource controller: php artisan make:controller PostController --module=blog --resource
This creates app-modules/blog/src/Http/Controllers/PostController.php with all resource methods (index, create, store, show, edit, update, destroy).
Create a view component
Generate a Blade component for displaying posts: php artisan make:component PostCard --module=blog
This creates:
app-modules/blog/src/View/Components/PostCard.php
app-modules/blog/resources/views/components/post-card.blade.php
You can use it in any view: < x-blog::post-card :post = " $post " />
Add routes
Edit app-modules/blog/routes/blog-routes.php to add your routes: app-modules/blog/routes/blog-routes.php
<? php
use Illuminate\Support\Facades\ Route ;
use Modules\Blog\Http\Controllers\ PostController ;
Route :: middleware ( 'web' ) -> prefix ( 'blog' ) -> group ( function () {
Route :: resource ( 'posts' , PostController :: class );
});
Run migrations
Your module’s migrations are automatically discovered. Run them:
Using Module Features
Now that you’ve built some components, here’s how to use them:
Routes
Blade Components
Views
Translations
// Module routes are automatically loaded
// Visit /blog/posts in your browser
Available Make Commands
All Laravel make: commands support the --module option:
php artisan make:model Post --module=blog
php artisan make:migration create_posts_table --module=blog
php artisan make:factory PostFactory --module=blog
php artisan make:seeder PostSeeder --module=blog
php artisan make:controller PostController --module=blog
php artisan make:request StorePostRequest --module=blog
php artisan make:resource PostResource --module=blog
php artisan make:middleware CheckAuthor --module=blog
php artisan make:component Alert --module=blog
php artisan make:job PublishPost --module=blog
php artisan make:event PostPublished --module=blog
php artisan make:listener SendPostNotification --module=blog
php artisan make:mail PostPublished --module=blog
php artisan make:notification PostLiked --module=blog
Authorization & Validation
php artisan make:policy PostPolicy --module=blog
php artisan make:rule ValidSlug --module=blog
php artisan make:cast Slug --module=blog
php artisan make:command PublishScheduledPosts --module=blog
php artisan make:provider BlogServiceProvider --module=blog
php artisan make:observer PostObserver --module=blog
php artisan make:exception PostNotFoundException --module=blog
php artisan make:test PostTest --module=blog
Seeding Module Data
You can run seeders specific to your module:
# Run the module's DatabaseSeeder
php artisan db:seed --module=blog
# Run a specific seeder in the module
php artisan db:seed --class=PostSeeder --module=blog
Module Management Commands
Laravel Modular provides several utility commands:
List Modules
Cache Module Registry
Clear Module Cache
Sync Project Config
Performance Tip: Run php artisan modules:cache in production to cache module discovery and improve performance.
Next Steps
Now that you’ve created your first module, explore more advanced features:
Namespace Configuration Learn about advanced configuration options and customization
Module Components Deep dive into module-namespaced Blade components
Module Views Working with Blade templates and module namespaces
Plugin System Understand the extensible plugin architecture