Appearance
Building Module Filament Plugin
This guide will walk you through the process of building a module that integrates with Filament.
1. Creating the Plugin Class
Create a plugin class in the app/Filament/Plugin
directory of your module. The class should implement the Filament Plugin contract:
php
<?php
namespace Modules\YourModule\Filament\Plugin;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Modules\YourModule\Filament\Resources\YourResource;
use Modules\YourModule\Filament\Pages\YourPage;
use Modules\YourModule\Filament\Widgets\YourWidget;
class YourModulePlugin implements Plugin
{
public function getId(): string
{
return 'your-module';
}
public function register(Panel $panel): void
{
$panel
->resources([
YourResource::class
])
->pages([
YourPage::class
])
->widgets([
YourWidget::class,
]);
}
public function boot(Panel $panel): void
{
// Additional initialization logic
}
public static function make(): static
{
return app(static::class);
}
}
2. Module Configuration
In the module.json
file of your module, add the plugin class to the filament_plugins
array. This allows the Module Manager to automatically register the plugin:
json
{
"name": "YourModule",
"alias": "yourmodule",
"description": "Your module description",
"keywords": [],
"priority": 0,
"providers": ["Modules\\YourModule\\Providers\\YourModuleServiceProvider"],
"files": [],
"filament_plugins": [
"Modules\\YourModule\\Filament\\Plugin\\YourModulePlugin"
]
}
3. Plugin Class Breakdown
Key methods of the Filament Plugin class:
getId()
: Returns a unique identifier for the plugin.register()
: Configures panel resources, pages, widgets, routes, and other settings.boot()
: Provides additional initialization logic.make()
: Creates an instance of the plugin using the application container.
4. Advanced Plugin Features
Using CanRegisterPanelsProviders
If your module needs to register additional panel providers, you can use the CanRegisterPanelsProviders
trait:
php
<?php
namespace Modules\YourModule\Filament\Plugin;
use Filament\Contracts\Plugin;
use Filament\Panel;
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([
YourResource::class,
]);
// Register panel providers from your module
$this->registerPanels([
YourModulePanelProvider::class,
]);
}
// ...
}
Conditional Registration
You can conditionally register resources, pages, or widgets based on certain conditions:
php
public function register(Panel $panel): void
{
$resources = [
YourResource::class,
];
// Only register AdminResource if the user has admin privileges
if (auth()->user()?->hasRole('admin')) {
$resources[] = AdminResource::class;
}
$panel->resources($resources);
}
5. Best Practices
Directory Structure: Keep your Filament components organized in appropriate directories:
Filament/Resources/
- For resource classesFilament/Pages/
- For page classesFilament/Widgets/
- For widget classesFilament/Plugin/
- For plugin classes
Configuration: Use configuration files for settings that might need to be customized by the user.
Trait Usage: Leverage Module Manager traits like
HasConfigurableItems
to make your Filament resources configurable via config files.Multi-Tenancy: If your application uses multi-tenancy, ensure your resources properly respect tenant boundaries using the
HasTenant
trait on your models.Translations: Use Laravel's translation system for all user-facing text to support multiple languages.
Testing: Create tests for your Filament components to ensure they work correctly.