wandypurnomo/modularity
Laravel package for building modular applications with a clean module structure. Organize features into self-contained modules, manage module discovery and loading, and keep codebases scalable and maintainable.
Installation
composer require wandypurnomo/modularity
Publish the package configuration (if needed):
php artisan vendor:publish --provider="Wandypurnomo\Modularity\ModularityServiceProvider"
Basic Module Creation
Use the make:module Artisan command to scaffold a new module:
php artisan make:module [module-name] [--type=module|package]
Example:
php artisan make:module Blog --type=module
This generates:
Modules/Blog directory with:
Http/Controllers/ (for module-specific controllers)Providers/ (module service provider)Routes/ (module routes)Views/ (module-specific Blade views)Database/Migrations/ (module migrations)First Use Case: Isolated Routes
Define routes in Modules/Blog/Routes/web.php:
Route::prefix('blog')->group(function () {
Route::get('/', 'BlogController@index');
});
Register the module routes in Modules/Blog/Providers/BlogServiceProvider.php:
public function boot()
{
$this->loadRoutesFrom(__DIR__.'/../Routes/web.php');
}
config/modularity.php under providers.whenIsLoaded() to defer module initialization:
public function boot()
{
if ($this->app->isLoaded('Blog')) {
$this->loadViewsFrom(__DIR__.'/../Views', 'blog');
}
}
config/modules/blog.php.config('modules.blog.key');
php artisan vendor:publish --tag=blog-config
php artisan module:migrate Blog
php artisan module:migrate:rollback Blog
view('blog::template') for module-specific Blade files.$this->publishes([
__DIR__.'/../Resources/assets' => public_path('vendor/blog'),
], 'blog-assets');
config/modules/blog.php:
'dependencies' => ['Auth', 'Cache'],
modularity:load:
php artisan modularity:load Blog
BlogServiceProvider:
$this->app['router']->aliasMiddleware('blog.auth', \Modules\Blog\Http\Middleware\Authenticate::class);
Route::middleware(['blog.auth'])->group(function () {
// Protected routes
});
Modules/Blog/Console/Commands.BlogServiceProvider:
$this->commands([
\Modules\Blog\Console\Commands\BlogCommand::class,
]);
config/modularity.php:
'load_order' => ['Auth', 'Blog', 'Settings'],
php artisan route:clear
php artisan route:cache
php artisan route:list to debug route conflicts.UserController) will conflict.\Modules\Blog\Http\Controllers\UserController::class
BlogServiceProvider:
$this->app->bind(
\Modules\Blog\Contracts\PostRepository::class,
\Modules\Blog\Repositories\PostRepository::class
);
whenIsLoaded() to avoid binding errors:
if ($this->app->isLoaded('Blog')) {
$this->app->bind(...);
}
php artisan migrate may skip module migrations.php artisan module:migrate [module] for isolated migrations.php artisan vendor:publish --tag=blog-assets --link
\Wandypurnomo\Modularity\Facades\Modularity::loaded();
php artisan modularity:load Blog --verbose
ModuleCreator in app/Providers/ModularityServiceProvider.php:
$this->app->bind('module.creator', function () {
return new \App\Services\CustomModuleCreator();
});
ModuleLoaded):
\Wandypurnomo\Modularity\Events\ModuleLoaded::class
config/modularity.php:
'disabled' => ['DeprecatedModule'],
$this->app->shouldLoad('Blog');
$this->app->shouldLoad('Auth');
How can I help you explore Laravel packages today?