The plugin system is Laravel Modular’s extensibility mechanism that allows you to discover and register module resources automatically. Plugins are classes that implement a discovery pattern to find resources across all modules and handle their registration with Laravel.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/internachi/modular/llms.txt
Use this file to discover all available pages before exploring further.
How Plugins Work
Every plugin follows a two-phase lifecycle:- Discovery Phase: Scan module directories for specific resources
- Handling Phase: Register discovered resources with Laravel services
Plugin class and implement two core methods:
Creating a Custom Plugin
Here’s how to create a custom plugin that discovers and registers custom resources.use InterNACHI\Modular\Plugins\Plugin;
use InterNACHI\Modular\Support\FinderFactory;
use Illuminate\Support\Collection;
class CustomPlugin extends Plugin
{
public function discover(FinderFactory $finders): iterable
{
// Return discovered resources
}
public function handle(Collection $data): void
{
// Register resources with Laravel
}
}
public function discover(FinderFactory $finders): iterable
{
return $finders
->commandFileFinder() // Built-in finder for commands
->withModuleInfo()
->values()
->map(fn(ModuleFileInfo $file) => $file->fullyQualifiedClassName())
->filter($this->isValid(...));
}
public function handle(Collection $data): void
{
$data->each(function(string $class) {
// Register with Laravel service
app()->singleton($class);
});
}
Boot Lifecycle Attributes
Plugins can use PHP attributes to control when and how they boot:AfterResolving Attribute
Boot the plugin after a service is resolved from the container:AfterResolving attribute waits for the BladeCompiler to be resolved, then injects it as the $blade constructor parameter.
OnBoot Attribute
Boot the plugin immediately during application boot:Built-in Plugins
Laravel Modular includes several built-in plugins:ArtisanPlugin
ArtisanPlugin
Discovers and registers Artisan commands from modules.Source:
src/Plugins/ArtisanPlugin.php:31-43MigratorPlugin
MigratorPlugin
Discovers and registers database migration directories.Source:
src/Plugins/MigratorPlugin.php:19-30BladePlugin
BladePlugin
Discovers and registers Blade components from modules.Source:
src/Plugins/BladePlugin.php:19-52GatePlugin
GatePlugin
Discovers models and their associated policies, registering them with Laravel’s Gate.Source:
src/Plugins/GatePlugin.php:20-52FinderFactory Methods
TheFinderFactory provides built-in finders for common module resources:
| Method | Path Pattern | Use Case |
|---|---|---|
commandFileFinder() | */src/Console/Commands/*.php | Artisan commands |
migrationDirectoryFinder() | */database/migrations/ | Database migrations |
modelFileFinder() | */src/Models/*.php | Eloquent models |
bladeComponentFileFinder() | */src/View/Components/*.php | Blade component classes |
bladeComponentDirectoryFinder() | */src/View/Components/ | Blade component directories |
routeFileFinder() | */routes/*.php | Route files |
viewDirectoryFinder() | */resources/views/ | View directories |
langDirectoryFinder() | */resources/lang/ | Translation files |
listenerDirectoryFinder() | */src/Listeners/ | Event listeners |
factoryDirectoryFinder() | */database/factories/ | Model factories |
src/Support/FinderFactory.php
Plugin Registry
ThePluginRegistry manages plugin registration and resolution:
src/PluginRegistry.php:14-32
Plugins are resolved as singletons from the Laravel container, so they can inject dependencies through their constructors.
Best Practices
Idempotency: Ensure your plugin’s
handle() method can be called multiple times safely.Performance: Use the appropriate finder methods to minimize filesystem scanning. The
FinderFactory methods are optimized for specific directory structures.Error Handling: Handle cases where resources might not exist or be malformed gracefully.