- How do I split a large Laravel app into modules without breaking existing functionality?
- Use `php artisan module:make` to create modules (e.g., `AuthModule`, `BillingModule`) with their own controllers, models, and routes. Existing routes in `app/Http/` won’t conflict if you prefix module routes (e.g., `/auth/*`) or use middleware. Modules auto-register via the service provider, so no manual bootstrapping is needed.
- Can I use Laravel 11’s new features (e.g., app models, routes) inside modules?
- Yes. Modules fully support Laravel 11’s conventions, including app models (place them in `Modules/{Module}/App/Models/`) and route caching. The package aligns with Laravel’s latest architecture, so you’ll get all native features like optimized route loading and model binding.
- Do modules support Vite/Laravel Mix for asset compilation?
- Absolutely. Modules can have isolated Vite/Laravel Mix builds by defining module-specific entry points (e.g., `resources/js/modules/Auth.js`). Configure Vite to split builds by module or use shared builds with dynamic imports. The package doesn’t enforce a specific approach, giving you flexibility.
- How do I test a single module in isolation?
- Disable other modules during testing with `Module::disable('OtherModule')` or use PHPUnit’s `--filter` flag to run only module-specific tests. For database-backed activation, mock the `Module` facade or use `module:disable` in your test setup. Modules can also be tested as standalone packages with `composer require` in a test project.
- What’s the performance impact of using modules vs. a monolith?
- Minimal. Modules lazy-load, so there’s no runtime penalty. The only overhead is during `composer dump-autoload` (~50ms), which is negligible. Database-backed activation adds a single query per request, but file-based activation is zero-cost. Benchmark your specific use case, but most teams see no meaningful slowdown.
- Can I deploy modules independently (e.g., zero-downtime updates)?
- Yes, but requires database-backed activation (run `php artisan module:v6:migrate`) and feature flags. Enable/disable modules via the database or storage, then update code in stages. For shared databases, use transactions or sagas to manage cross-module consistency. Avoid this for modules with tight coupling.
- How do I share a model between modules (e.g., `User` in both `AuthModule` and `BillingModule`)?
- Place shared models in `app/Models/` (Laravel 11) or use a shared module (e.g., `CoreModule`). Avoid duplicating models in multiple modules. For module-specific logic, extend the shared model or use traits. The package doesn’t restrict model placement, so follow Laravel’s conventions for shared resources.
- What’s the difference between file-based and database-backed module activation?
- File-based activation uses `storage/framework/modules.php` (zero-config, default) and is ideal for simple apps. Database-backed activation (via `module:v6:migrate`) adds a `modules` table for dynamic enabling/disabling, useful for feature flags or multi-tenant setups. Switch between them by publishing the config and running the migration.
- How do I add a module-specific migration or config?
- Use `php artisan module:make:migration` or `module:publish` to generate module-specific migrations/configs. Place them in `Modules/{Module}/Database/Migrations/` or `config/modules/{module}.php`. Publish them with `php artisan module:publish --module={Module}`. Migrations run alongside Laravel’s default migrations.
- Are there alternatives to `nwidart/laravel-modules` for Laravel modularity?
- Yes, but this package is the most mature and Laravel-idiomatic. Alternatives include **spatie/laravel-modules** (simpler but less feature-rich) or **orchid/platform** (full admin panel with modules). For microservices, consider **Laravel Horizon** + **Lumen** or **Spatie’s API resources**. This package stands out for its Artisan tooling, Vite support, and Laravel 11 compatibility.