Installation:
composer require zingle-com/laravel-modules
Register the ModuleServiceProvider in config/app.php after Laravel core providers but before custom providers.
Publish Assets:
php artisan vendor:publish --provider="ZingleCom\LaravelModules\ModuleServiceProvider"
This generates a config/modules.php file with default settings.
Create a Module:
php artisan module:make Blog
This scaffolds a module directory (app/Modules/Blog) with:
Module.php (core class)Providers/ (for service providers)Http/ (routes, controllers)Resources/ (views, assets)Register the Module:
Add the module to config/modules.php under modules array:
'modules' => [
'Blog' => [
'path' => 'app/Modules/Blog',
'is_core' => false,
'providers' => [
'BlogServiceProvider',
],
'routes' => [
'web' => 'routes/web.php',
],
],
],
First Use Case:
Load module routes in routes/web.php:
Route::module('Blog');
Module Initialization:
ModuleServiceProvider when registered in config/modules.php.Module::load('Blog') to manually load a module (e.g., in a command or middleware).Service Providers:
Providers/ (e.g., BlogServiceProvider.php).config/modules.php under the module’s providers key.class BlogServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton('blog.repository', function () {
return new BlogRepository();
});
}
}
Routing:
Http/routes/web.php (or api.php).Route::module('ModuleName') in your main web.php.Route::module('Blog')->group(function () { ... }) for scoped routes.Views and Assets:
resources/views/modules/{ModuleName}/.@module('Blog') directive in Blade templates to load module-specific views:
@module('Blog')
@include('blog::home')
@endmodule
php artisan module:publish-assets Blog.Middleware:
Http/Kernel.php:
protected $moduleMiddleware = [
'Blog' => [
\App\Modules\Blog\Http\Middleware\Authenticate::class,
],
];
config/modules.php:
'Blog' => [
'middleware' => ['auth.blog'],
],
Commands and Jobs:
Console/Commands/ and jobs in Jobs/ within the module.BlogServiceProvider:
$this->commands([
\App\Modules\Blog\Console\Commands\BackupCommand::class,
]);
Database Migrations:
Database/Migrations/ within the module.php artisan module:migrate Blog
php artisan module:migrate:rollback Blog
Testing:
php artisan module:test Blog to run module-specific tests.Tests/Module/Blog/.Dependency Management:
Module::dependencies() to define module dependencies (e.g., Blog depends on Auth):
// app/Modules/Blog/Module.php
public function dependencies() {
return ['Auth'];
}
Configuration:
php artisan module:publish-config Blog
config('blog.key').Event Handling:
Module.php:
public function boot() {
event(new ModuleBooted($this));
}
EventServiceProvider.APIs and Controllers:
Route::module('Blog')->api() for API routes.BaseController from the module or use Laravel’s built-in controllers.Localization:
Resources/lang/ within the module.__('blog::key').Caching:
php artisan module:clear-cache Blog
Service Provider Loading Order:
ModuleServiceProvider in config/modules.php.Class not found for module providers if loaded out of order.Route Caching:
php artisan route:clear
Route::module().Namespace Conflicts:
Auth, Cache).UserAuth instead of Auth).Middleware Not Triggering:
config/modules.php under middleware.Kernel.php (if custom middleware is used).Route::getRoutes() to verify middleware is attached.Asset Publishing Issues:
php artisan module:publish-assets Blog after compiling assets (e.g., after npm run dev).Module Auto-Loading:
is_core: true in config/modules.php are loaded automatically on app boot.Module::load('Blog') or Route::module().Database Seeders:
Database/Seeders/ and registered in BlogServiceProvider:
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
$this->loadSeedersFrom(__DIR__.'/../Database/Seeders');
php artisan module:seed Blog
Testing Modules:
$this->partialMock(Module::class, 'dependencies')
->shouldReturn(['MockDependency']);
php artisan module:test Blog --env=testing for isolated testing.Check Module Status:
php artisan module:list
Enable Debugging:
debug to true in config/modules.php to log module events:
'debug' => env('MODULES_DEBUG', false),
Common Errors and Fixes:
| Error | Solution |
|---|---|
Class 'Module' not found |
Ensure use ZingleCom\LaravelModules\Module; is added to your file. |
Module not registered |
Add the module to config/modules.php. |
Route [module.route] not defined |
Verify Route::module('ModuleName') is called in your main routes file. |
Table not found (migrations) |
Run php artisan module:migrate ModuleName. |
View not found |
Ensure the view exists in resources/views/modules/ModuleName/. |
Log Module Events:
Module.php to log events:
public function boot() {
\Log::info("Module {$this->getName()} booted.");
}
Custom Module Classes:
ZingleCom\LaravelModules\Module to add custom logic:
class BlogModule extends Module {
public function extraLogic() {
// Custom initialization
}
}
Dynamic Module Loading:
ModuleServiceProvider:
if ($this->app->environment('local')) {
Module
How can I help you explore Laravel packages today?