nwidart/laravel-modules
Laravel package for structuring large apps into self-contained modules (like mini packages) with their own controllers, models, views, routes, and config. Includes Artisan generators, module discovery, and configuration for organizing and scaling projects.
AuthModule, BillingModule). Each module encapsulates its own:
Modules\Auth\...), preventing naming collisions while allowing global access via Laravel’s service container.composer-merge-plugin for autoloading (v11+).module:make, module:publish, module:seed) and lifecycle management (enable/disable modules).resources/js/modules/Auth.js).module:v6:migrate).| Risk Area | Mitigation |
|---|---|
| Performance Overhead | Minimal runtime impact (modules lazy-load; no global autoloading by default). Autoloading adds ~50ms to composer dump-autoload but no runtime penalty. |
| Namespace Pollution | Strict module namespacing (e.g., Modules\Auth\...) prevents collisions. Global helpers (e.g., module_path()) are scoped. |
| Migration Complexity | Database-backed activation requires a one-time migration (module:v6:migrate). File-based activation is zero-config. |
| Asset Compilation | Vite/Laravel Mix support is robust but requires explicit module-specific config (e.g., vite.config.js per module). |
| Testing Isolation | Modules can be tested in isolation using Module::disable() or via PHPUnit’s --filter flag. Cross-module dependencies require explicit setup. |
| Legacy Code Conflicts | Existing routes/controllers in app/Http/ may conflict with module routes. Solution: Use module-specific middleware or route prefixes (e.g., /auth/*). |
| Package Maturity | Actively maintained (13.x for Laravel 11/12), with 6K+ stars and Discord community. Changelog shows steady feature additions (e.g., make-action, make-event-provider). |
Billing, Inventory) or technical layers (e.g., Auth, Notifications)?BillingModule and InventoryModule may need ACID transactions for order processing.X-Module-ID headers or Laravel tags.Auth, Payments) first.pdo_mysql, fileinfo).Modules\MongoDb\...).cache()->tags(['module-auth'])).| Phase | Actions |
|---|---|
| Assessment | Audit existing codebase for: |
Route::get('/login', ...)).User model used across Auth and Profile modules).users table shared vs. module-specific). |
| Scaffolding | 1. Install package: composer require nwidart/laravel-modules.php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider".composer.json for autoloading (if needed).composer dump-autoload. |
| Module Creation | Start with high-cohesion, low-coupling components:php artisan module:make Auth --controller --migration --resource --seed
php artisan module:make Billing --controller --migration --seed
--api for API-only modules (e.g., Billing).--web for Blade-based modules (e.g., Auth). |
| Incremental Refactor | 1. Extract Routes: Move routes from routes/web.php to module-specific RouteServiceProviders.Modules\Auth\Http\Controllers\LoginController).use App\Models\User with use Modules\Auth\Models\User.Module::disable('Auth') to test other modules. |
| Database Migration | If using database-backed activation:php artisan module:v6:migrate
config/modules.php to set 'activator' => 'database'. |
| Asset Pipeline | Configure Vite/Laravel Mix per module:// vite.config.js
export default defineConfig({
build: {
rollupOptions: {
input: {
auth: path.resolve(__dirname, 'resources/js/modules/auth.js'),
billing: path.resolve(__dirname, 'resources/js/modules/billing.js'),
},
},
},
});
``` |
| **Deployment** | 1. **Feature Flags**: Use Laravel’s `config('modules.enabled')` to toggle modules.
2. **Zero-Downtime**: Deploy module updates independently (if using database-backed activation).
3. **Rollback**: Disable module via `php artisan module:disable Billing` if issues arise. |
### **Compatibility**
| **Component** | **Compatibility Notes** |
|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Laravel Packages** | Most packages work out-of-the-box (e.g., Spatie Media Library, Laravel Nova). Some may require module-specific configuration (e.g., `config('media-library.disk')` per module). |
| **Middleware** | Module
How can I help you explore Laravel packages today?