Self-Registering Filament Plugins
Let's imagine I am developing a package cms (based on Filament). Now I want to offer an additional package called blog.
In my laravel project now I add the two dependencies cms and blog.
The cms has the FilamentServiceProvider to register the panel.
The blog package contains a Filament plugin.
Is there anyway, that the blog package registers it's BlogPlugin "itself" into the Filament panel?
So, both, the laravel project nor the cms package doesn't need to know anything about the blog package?
Thank you 🙂
9 Replies
By using
$panel = filament()->getPanel('<id>')
I can actually get the Panel object – But is there a way to register a plugin after the creation of the panel?If CMS can exist without blog I’d stick with manually registration. Otherwise just add it to CMS.
CMS can exist without it. Blog ist just an "addon".
I want to avoid manual registration. I just want to
composer install blog
and it should be ready to go like with most of other Laravel packages.
With
if ($currentPanel = filament()->getPanel('xxx')) {
$currentPanel->plugins([
BlogPlugin::make(),
]);
}
I am quite near I think. But, it results into:
Route [filament.xxx.resources.posts.index] not defined.
When opening the admin.
So, the plugin got registered somehow. But not completely...Are you running this is boot or register?
Currently in boot()
Doing it in register(), as panel wasn' created yet:

Try it in packageBoot() instead. Just a thought.
I’m thinking it’s a laravel lifecycle issue though. Which is why manual registration usually works.
Manual registration is not an option for me.
But I found a soltuion
solution*
Using this, in the blog's serviceprovider's register():
$this->app->afterResolving(FilamentPluginRegistry::class, function ($service) {
$service->register('blog', BlogPlugin::class);
});
and in FilamentServiceProvider's panel():
->plugins(
array_map(
fn ($plugin) => $plugin::make(),
app(FilamentPluginRegistry::class)->all()
)
);
(Of course, FilamentPluginRegistry need to be a singleton.
The afterResolving() is required as otherwise the FilamentPluginRegistry may is not declaed as singleton from the AppServiceProvider, when the blog's provider's register() got triggered.