nwidart/laravel-modules
Modularize large Laravel apps with nwidart/laravel-modules. Create self-contained modules (controllers, models, views, routes, config) with Artisan generators, module discovery, enabling/disabling, and per-module resources—tested and maintained across modern Laravel versions.
AuthModule, BillingModule) aligns with business capabilities. It enforces cohesion (related logic grouped) and loose coupling (modules interact via contracts/APIs).Http/Controllers, Database/Migrations), which may conflict with existing legacy codebases.module:make commands (controllers, models, etc.) reduce boilerplate.App\Http\Controllers may clash with module-generated paths (e.g., Modules/Auth/Http/Controllers)./login) require explicit namespace resolution (e.g., Route::module('Auth')->get(...)).Nwidart\Modules\Module or use the ModuleServiceProvider wrapper.auth_users) to avoid conflicts.--direction flag (e.g., module:seed --direction=down) helps with rollbacks, but shared seed data (e.g., roles) needs coordination.| Risk Area | Severity | Mitigation |
|---|---|---|
| Performance Overhead | Medium | File-based module activation (module_statuses.json) scales poorly for >100 modules. Database activator (v6+) reduces this but adds complexity. |
| Dependency Spaghetti | High | Modules sharing Laravel core (e.g., Auth, Cache) risk circular dependencies. Use interfaces/contracts for module-to-module communication. |
| Asset Pipeline Fragments | Medium | Vite/Laravel Mix support is module-aware but requires manual config for shared assets (e.g., global CSS/JS). |
| Testing Isolation | High | Modules share Laravel’s kernel, making unit testing harder. Use --module flag in phpunit.xml to scope tests. |
| Upgrade Path | Critical | Breaking changes (e.g., v6’s File Activator) require migration scripts (php artisan module:v6:migrate). |
Billing and Payments are separate modules?")Laravel\Cashier) be managed across modules?| Phase | Steps | Tools/Commands |
|---|---|---|
| Assessment | Audit existing code for namespace conflicts, shared routes, and global service providers. | composer why-not nwidart/laravel-modules |
| Scaffold Setup | Initialize modules in a dedicated modules/ directory (or custom path). |
composer require nwidart/laravel-modules |
| Configuration | Publish config and stubs; configure module namespace, asset paths, and database activator. | php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" |
| Module Extraction | Refactor controllers, models, and routes into modules. Use --module flag for make:controller, make:model, etc. |
php artisan module:make Auth --controller --model --migration --resource |
| Route Isolation | Replace routes/web.php with module-specific RouteServiceProvider or use Route::module('Auth')->.... |
php artisan module:route-provider Auth |
| Asset Pipeline | Configure Vite/Mix per module or use a global build with module-specific entry points. | Update vite.config.js or webpack.mix.js |
| Database Migration | Prefix module tables (e.g., auth_users) and update seeders with --direction flags. |
php artisan module:seed Auth --class=AuthDatabaseSeeder --direction=up |
| Testing | Scope tests to modules using --module in phpunit.xml or create module-specific test suites. |
php artisan make:test AuthTest --module=Auth |
| Deployment | Use module activation (php artisan module:enable Auth) for feature flags or database activator for runtime control. |
php artisan module:enable Auth |
module:make handles scaffolding.Blog, Settings) before core domains (Auth, Payments).AuthContracts\Authenticator).UserProfile from Auth).composer dump-autoload --optimize.php artisan module:disable Auth).Modules\Auth\Http\Controllers\ProfileController) increase import complexity.How can I help you explore Laravel packages today?