Overview
TheModulesPlugin is the foundational plugin that discovers all modules in your application. It scans the modules directory, reads each module’s composer.json file, and creates ModuleConfig instances that are used throughout the system.
How It Works
This plugin is responsible for the initial module discovery phase. All other plugins depend on the modules that this plugin discovers.Discovery Process
- Scan Directory: Looks in the configured modules directory (default:
app-modules/) - Read Composer Files: Reads each module’s
composer.jsonfor PSR-4 autoload configuration - Extract Metadata: Extracts the module name, base path, and namespaces
- Create Configs: Creates
ModuleConfiginstances for each module
Source Code
Handling Phase
The handling phase converts the discovered data intoModuleConfig objects:
Expected Module Structure
Each module must have acomposer.json file with PSR-4 autoload configuration:
Example composer.json
What Gets Discovered
For each module, the plugin extracts:- Name: The directory name (e.g.,
blog,user-management) - Base Path: Absolute path to the module directory
- Namespaces: PSR-4 namespace mappings from
composer.json
Multiple Namespace Support
Modules can define multiple PSR-4 namespaces:ModuleConfig Class
The plugin returns a collection ofModuleConfig instances:
Integration with Other Plugins
All other plugins receive module information from this plugin:- ModulesPlugin discovers modules
- Modules are stored in the registry
- Other plugins query the registry to find module-specific resources:
ArtisanPluginfinds commands in discovered modulesBladePluginfinds components in discovered modulesRoutesPluginfinds route files in discovered modules- And so on…
Module Registry
The discovered modules are stored in theModuleRegistry:
Caching
Module discovery can be cached for production:The ModulesPlugin is always the first plugin to run, as all other plugins depend on knowing which modules exist.
Troubleshooting
Module Not Discovered
If a module isn’t being discovered:- Check composer.json exists in the module directory
- Verify PSR-4 autoload is configured
- Clear cache:
php artisan modules:clear - Check directory structure matches configured path
Namespace Resolution Issues
If classes aren’t being found:- Run composer update to update autoloader
- Check PSR-4 mapping matches actual directory structure
- Verify namespace in PHP files matches composer.json
See Also
- ModuleRegistry - Accessing discovered modules
- ModuleConfig - Module configuration object
- Module Architecture - How module discovery works
- Creating Modules - Setting up new modules