Appearance
CanRegisterPanelsProviders Trait
The CanRegisterPanelsProviders trait provides functionality to register panel providers inside modules, allowing for more modular and organized Filament panel configuration.
Features
This trait offers:
- Easy registration of PanelProvider classes
- Type checking to ensure valid providers are registered
- Integration with the Filament panel registry
Methods
registerPanels(array $providers): void
Registers an array of panel provider classes with the application.
Parameters:
$providers(array): An array of PanelProvider class names to register
Behavior:
- Validates that each provider is a subclass of
Filament\PanelProvider - Instantiates each provider
- Creates a new panel instance for each provider
- Registers the panel with the Filament panel registry
- Validates that each provider is a subclass of
Usage Example
Here's how to use the CanRegisterPanelsProviders trait in a Filament plugin:
php
<?php
namespace Modules\YourModule\Filament\Plugin;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Modules\YourModule\Filament\YourModulePanelProvider;
use ModuleManager\ModuleManager\Concerns\CanRegisterPanelsProviders;
class YourModulePlugin implements Plugin
{
use CanRegisterPanelsProviders;
public function getId(): string
{
return 'your-module';
}
public function register(Panel $panel): void
{
// Register resources, pages, etc.
$panel->resources([
YourModuleResource::class,
]);
// Register panel providers from your module
$this->registerPanels([
YourModulePanelProvider::class,
]);
}
public function boot(Panel $panel): void
{
// Boot functionality...
}
}Notes
- This trait is particularly useful when a module needs to register its own panel or multiple panels within the main application
- It automatically handles the instantiation and registration process with the correct dependency injection
- An
InvalidArgumentExceptionwill be thrown if a non-PanelProvider class is passed to theregisterPanels()method