F
Filament13mo ago
flex

Register Resources from a Plugin without adding it as Plugin in Panel?

Hi, in v2 I used PluginServiceProvider::$resources to register new resources. So, I just needed to install the package and the resource was available. While upgrading to v3 I'm extending PackageServiceProvider now, but the resources don't get registered: Is there any option to register resources in v3 from a plugin without the need to register it in the "parent" panel (as this is in a package for me too)? Thank you!
<?php
namespace X\Blog\Providers;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use X\Blog\Filament\Resources\PostResource;

class FilamentServiceProvider extends PackageServiceProvider
{
public static string $name = 'X/blog';

protected array $resources = [
PostResource::class,
];

public function configurePackage(Package $package): void
{
$package->name('X/blog');
}
}
<?php
namespace X\Blog\Providers;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use X\Blog\Filament\Resources\PostResource;

class FilamentServiceProvider extends PackageServiceProvider
{
public static string $name = 'X/blog';

protected array $resources = [
PostResource::class,
];

public function configurePackage(Package $package): void
{
$package->name('X/blog');
}
}
23 Replies
flex
flexOP13mo ago
One idea was to:
public function packageBooted(): void
{
filament()->registerResources([
PostResource::class
]);
}
public function packageBooted(): void
{
filament()->registerResources([
PostResource::class
]);
}
and seems like "something" is happening with the resource but is resulting into
[2023-11-04 15:07:13] local.ERROR: Route [filament.admin.resources.posts.index] not defined. {"view":{"view":"/Users/x/Documents/Repositories/x/vendor/filament/filament/resources/views/components/layout/index.blade.php","data"....
[2023-11-04 15:07:13] local.ERROR: Route [filament.admin.resources.posts.index] not defined. {"view":{"view":"/Users/x/Documents/Repositories/x/vendor/filament/filament/resources/views/components/layout/index.blade.php","data"....
Lara Zeus
Lara Zeus13mo ago
GitHub
plugin-skeleton/src/SkeletonPlugin.php at 3.x · filamentphp/plugin-...
A package skeleton for developing Filament plugins. - filamentphp/plugin-skeleton
flex
flexOP13mo ago
No, but if I create a Plugin, I need to explicitly load it in the panel or? What, if the panel is created by another package and I don't know which plugins I will use later in this package? Or can I use a plugin without the need of $panel->plugin() ?
Lara Zeus
Lara Zeus13mo ago
for a plugin: everyone using it has to register it in the panel provider
->plugins([
SkyPlugin::make(),
])
->plugins([
SkyPlugin::make(),
])
and all the resources in SkyPlugin will be available for them if the panel is created by another package, I guess the user can extend it and add the plugin to it then register it instead of the (package panel)
flex
flexOP13mo ago
Yes, that's how I understand the new plugin development. But may you have an idea for my scenario? I have one "core" package which generates the panel with couple of resources, etc.. Everything is fine here. I plan to use this package in different laravel projects. Now, for some, but not for all laravel projects I want to extend it by using the core-package PLUS a blog-package. Or maybe a shop-package or whatever. To extend the functionality. So panel provider is in a package too. With v2 it worked really well. Installed the blog/shop package and register resources in their providers. And I hope that this is possible in v3 in any way too? sorry, I don't understand your last sentence (Is it an option to get the default panel from the blog provider and call plugin() on it?)
Lara Zeus
Lara Zeus13mo ago
do you want blog/shop package in one panel?
flex
flexOP13mo ago
they should extend the core's panel there is only one panel blog/shop just bring some resources, controllers, views, etc...
Lara Zeus
Lara Zeus13mo ago
good then, it will work I have multiple packages I install them in one app added it to the AdminPanelProvider and they all there your package only have to defiend the plugin class I mentioned it earlier and in it you can set the resources you want
flex
flexOP13mo ago
but then the AdminPanelProvider need to be done in the laravel application? not in the core package? thank you a lot btw, for taking time ❤️
Lara Zeus
Lara Zeus13mo ago
yes np 🙂
Lara Zeus
Lara Zeus13mo ago
this my package https://github.com/lara-zeus/sky/blob/3.x/src/SkyPlugin.php and in my app AdminPanelProvider
->plugins([
WindPlugin::make(),
SkyPlugin::make(),
])
->plugins([
WindPlugin::make(),
SkyPlugin::make(),
])
GitHub
sky/src/SkyPlugin.php at 3.x · lara-zeus/sky
CMS for your website. it include posts, pages, tags, and categories. with a frontend scaffolding ready to use - lara-zeus/sky
flex
flexOP13mo ago
Hm, I hoped to continue the logic from v2. That I can "hook in" the resources from the blog package itself and don't need to make any adjustments somewhere else. This way I only had to composer install the package and functionality was fully ready. Would be great to avoid the panel provder in the laravel application to keep it as small as possible. But seems to be difficult? yes, I understand. But for now, I fortunately don't have the panel provider in the app. only in the core.
Lara Zeus
Lara Zeus13mo ago
you have to created in the panel provder also it will be created when you install filament so. and anyone using third party plugins have to register it in ->plugins([
flex
flexOP13mo ago
But using panel provider in my "core" package works pretty well.. except for those blog plugin I have the FilamentServiceProvider in my core package, not in the app My app almost knows nothing about Filament. No composer reuqirements, no Filament resources, etc.. everything about Filament is in my core package.
flex
flexOP13mo ago
And so far I just did this in my blog-package an evoila, PostResource was available in the Filament. Didn't expect that it's so different in V3
No description
Lara Zeus
Lara Zeus13mo ago
did you register FilamentServiceProvider in the app? in config/app.php
flex
flexOP13mo ago
just in the blog package's providers object
Lara Zeus
Lara Zeus13mo ago
also you can ship entire plugin in a package maybe useful in your case but it also requires your app to register the provider https://filamentphp.com/docs/3.x/panels/plugins#distributing-a-panel-in-a-plugin
flex
flexOP13mo ago
yes, thats what I am doing at the moment core package delivers the panel blog package should add resources to the core's panel 😛 may I can try to use getDefaultPanel() from the blog package's provder and then add the plugin()
Lara Zeus
Lara Zeus13mo ago
ummm 🙂 I think you core have to use the blog in the provider could work too
flex
flexOP13mo ago
but then it wouldn't make sense to put blog code into own package. i try to put all the optional features into packages. blog and shop are just some example. and I don't want to force the projects to use the blog, etc.. but yeah, will try with getDefaultPanel(), didn't yet I think this works in general and may is a good approach for some scenarios. Unfortunately I still get
Route [filament.admin.resources.posts.index] not defined.
Route [filament.admin.resources.posts.index] not defined.
from somwhere in getNavigation() of panel. So panel defnitely take aware of the BlogPlugin. You know if I need to register any additional routes now? So far it was enough to register the resource and the resource contains all the pages in getPages().
public function packageBooted(): void
{
Filament::getDefaultPanel()
->plugin(new BlogPlugin());
}
public function packageBooted(): void
{
Filament::getDefaultPanel()
->plugin(new BlogPlugin());
}
`
Lara Zeus
Lara Zeus13mo ago
not sure why, filament automatically registers the resource routes, so I think this means filament didnt register the posts resource routes for some reason!
flex
flexOP13mo ago
hm hm...
Want results from more Discord servers?
Add your server