This package provides a simple yet powerful structure for organizing Laravel applications into independent modules, allowing for a cleaner, more scalable, and maintainable architecture.
Each module is a mini Laravel application with its own routes, views, lang, configs, console, events, policies, and observers, all automatically managed by the ModuleServiceProvider
.
Add the package to your Laravel project:
composer require pijler/laravel-modules
Make sure your composer.json
autoload includes the Modules\
namespace:
"autoload": {
"psr-4": {
"Modules\\": "modules/"
}
}
Then run:
composer dump-autoload
Each module should follow the structure below inside the modules/
directory:
modules/
βββ Blog/
βββ app/
β βββ Console/
β β βββ Kernel.php
β βββ Http/
β β βββ Controllers/
β β βββ Middleware/
β βββ Providers/
β βββ AppServiceProvider.php
β βββ RouteServiceProvider.php
βββ config/
β βββ config.php
β βββ another.php
βββ lang/
β βββ en-US.json
β βββ pt_BR.json
βββ resources/
β βββ views/
β βββ index.blade.php
βββ routes/
βββ web.php
Each module must contain an AppServiceProvider
that extends the base ModuleServiceProvider
class.
Example:
<?php
namespace Blog\Providers;
use Modules\ModuleServiceProvider;
class AppServiceProvider extends ModuleServiceProvider
{
/**
* The module slug.
*/
protected string $slug = 'blog';
/**
* The module name.
*/
protected string $module = 'Blog';
/**
* Boot the gates for the module.
*/
protected function bootGates(): void
{
//
}
/**
* Boot the events for the module.
*/
protected function bootEvents(): void
{
//
}
/**
* Boot the policies for the module.
*/
protected function bootPolicies(): void
{
//
}
/**
* Boot the observers for the module.
*/
protected function bootObservers(): void
{
//
}
}
Then, register the provider in bootstrap/providers.php
:
return [
/*
* Modules Service Providers...
*/
Blog\Providers\AppServiceProvider::class,
];
Add a namespace mapping for the module in your composer.json
:
"autoload": {
"psr-4": {
"Blog\\": "modules/Blog/app/"
}
}
The base ModuleServiceProvider
automatically handles the booting of all module resources.
Automatically loads views from:
resources/views
And makes them available under the module namespace ($slug
):
@include('blog::index')
Supports both JSON and PHP translation files.
Automatically loads from:
lang/
Usage examples:
trans('Hello world'); // JSON
trans('blog::messages.welcome'); // PHP
Automatically loads and merges:
- A single
config.php
file - Or multiple files inside the
config/
directory
They can be accessed via config("{$slug}.key")
.
If a app/Console/Kernel.php
exists within the module, the provider:
- Registers Artisan commands located in the /Commands directory.
- Registers scheduled tasks defined in the
schedule(Schedule $schedule)
method.
Example:
namespace Blog\Console;
use Illuminate\Console\Scheduling\Schedule;
use Modules\BaseKernel;
class Kernel extends BaseKernel
{
/**
* Define the application's command schedule.
*/
public function schedule(Schedule $schedule): void
{
$schedule->command('blog:clean-posts')->daily();
}
/**
* Register the commands for the application.
*/
public function commands(): array
{
return $this->load(__DIR__.'/Commands');
}
}
The provider automatically attempts to execute the following methods, if they exist:
bootGates()
bootEvents()
bootPolicies()
bootObservers()
This allows additional resources to be registered without overriding boot()
.
Checks if a module exists inside the modules/
directory.
Name | Type | Description |
---|---|---|
$module |
string |
The name of the module to check. |
Type | Description |
---|---|
bool |
true if the module exists, false otherwise. |
if (module_has('Users')) {
// The "Users" module exists
}
Returns the full path to a module directory inside modules/
.
Name | Type | Description |
---|---|---|
$module |
string |
The name of the module. |
$path |
string |
Additional path within the module (optional). |
Type | Description |
---|---|
string |
The absolute path to the module directory. |
$path = module_path('Users');
// Returns something like: /var/www/app/modules/Users
$configPath = module_path('Users', 'config/module.php');
// Returns: /var/www/app/modules/Users/config/module.php
β
Modular and independent organization
β
Automatic loading of configs, views, and translations
β
Native integration with Scheduler and Artisan
β
Dynamic boot for gates, policies, observers, and events
β
Fully compatible with the Laravel ecosystem
Open-source under the MIT license.