Overview
Laravel Modular provides a lighter-weight alternative tonwidart/laravel-modules that follows Laravel conventions more closely. This guide will help you understand the differences and migrate your existing modules.
Key Differences
Philosophy
Laravel Modular is designed with different goals in mind:Laravel Modular focuses on organization rather than dynamic module management. If you need to dynamically enable/disable modules at runtime (common in CMS applications),
nwidart/laravel-modules may be a better fit.| Aspect | nwidart/laravel-modules | Laravel Modular |
|---|---|---|
| Directory Structure | Custom module structure | Standard Laravel conventions |
| Autoloading | Custom autoloader | Composer path repositories |
| Discovery | Custom discovery system | Laravel package discovery |
| Dynamic Loading | Enable/disable modules at runtime | All modules always loaded |
| Weight | Full-featured CMS solution | Minimal organizational tooling |
Technical Approach
nwidart/laravel-modules uses its own directory structure and conventions, making it powerful but requiring a learning curve beyond Laravel itself. Laravel Modular leverages:- Composer path repositories for autoloading
- Laravel package discovery for initialization
- Standard Laravel directory conventions within modules
Migration Steps
Install Laravel Modular
Install the package via Composer:Laravel will auto-discover the package automatically.
Publish and Configure
Publish the configuration file:Edit
config/app-modules.php to set your namespace:Restructure Module Directories
Laravel Modular uses Laravel package conventions. You’ll need to restructure your modules:Before (nwidart structure):After (Laravel Modular structure):
Update Directory Mappings
Map your nwidart directories to Laravel Modular conventions:
| nwidart | Laravel Modular | Notes |
|---|---|---|
Entities/ | src/Models/ | Follow Laravel conventions |
Http/Controllers/ | src/Http/Controllers/ | Same structure |
Console/ | src/Console/ | Commands auto-discovered |
Providers/ | src/Providers/ | Optional, auto-discovery handles most cases |
Config/ | config/ | Can be published via service provider |
Database/Migrations/ | database/migrations/ | Auto-discovered |
Database/Seeders/ | database/seeders/ | Use --module flag |
Resources/views/ | resources/views/ | Namespaced automatically |
Resources/lang/ | resources/lang/ | Namespaced automatically |
Create composer.json for Each Module
Each module needs a Example
composer.json file. You can generate new modules to see the structure:composer.json:Update Application composer.json
Add the module as a path repository and require it:Run Composer update:
Migrate Service Providers
Service providers are optional in Laravel Modular due to auto-discovery, but if you need one:Before (nwidart):After (Laravel Modular):
Update View and Translation References
Both packages use namespaced views and translations, but ensure your references match:Views:Translations:
Update Blade Components
Laravel Modular auto-discovers Blade components with namespacing:
| File | Component |
|---|---|
src/View/Components/Alert.php | <x-blog::alert /> |
resources/components/button.blade.php | <x-blog::button /> |
Run Sync Command
Update your project configuration:This will:
- Add modules to
phpunit.xml - Update PhpStorm configuration
- Ensure proper IDE support
Feature Comparison
Module Status (Enable/Disable)
Module Status (Enable/Disable)
nwidart: Modules can be enabled/disabled at runtimeLaravel Modular: All modules are always loaded (by design)If you need dynamic module loading, consider:
- Feature flags for functionality
- Route middleware for access control
- Staying with nwidart/laravel-modules
Module Commands
Module Commands
nwidart commands:
php artisan module:makephp artisan module:make-controllerphp artisan module:make-model- etc.
php artisan make:modulephp artisan make:controller --module=php artisan make:model --module=- Uses standard Laravel
make:commands with--moduleflag
Assets and Publishing
Assets and Publishing
nwidart: Built-in asset publishing systemLaravel Modular: Use standard Laravel asset publishing in your service provider:
Module Dependencies
Module Dependencies
nwidart: Custom dependency system in
module.jsonLaravel Modular: Use standard Composer dependencies in composer.json:Benefits of Laravel Modular
Follows Laravel Conventions
You can use all standard Laravel commands and patterns without learning module-specific syntax:Lighter Weight
Laravel Modular adds minimal overhead:- No custom autoloading
- No runtime module status checks
- No module-specific configuration formats
Better IDE Support
Because modules are Composer packages:- Full IDE autocomplete
- Native PHPStan/Psalm support
- Standard debugging tools work immediately
Standard Package Ecosystem
Modules can be easily extracted to standalone packages:Common Issues
Getting Help
If you encounter issues during migration:- Check the GitHub repository for similar issues
- Review the comparison section in the README
- Ensure your use case aligns with Laravel Modular’s organizational focus
When to Stay with nwidart
Consider staying withnwidart/laravel-modules if you:
- Need to dynamically enable/disable modules at runtime
- Are building a CMS with plugin architecture
- Require module-specific permissions and access control
- Have heavily invested in nwidart’s conventions and tooling