Custom resources directories
Has a way to create resources in custom folders?
Im using a modular architeture, for example, i have 3 different models:
base_path/modules/User
,
base_path/modules/Factory
,
base_path/modules/Car
,
And inside each module, i have a folder called filament/resources
, and inside each resources i have the respective Resource.
Is there any way to go through all the module folders and “attach” the resources?
In filament v2, this provider this provider was working:
24 Replies
if you register the entire modules directory as a resources directory, we will scan the whole thing and look for resources
in production make sure you use filament component caching to make sure this process is fast
With the code above the resources works fine, but the problem is with routes. If a panel has admin, the generated route ill contain filament.admin... in route name
not sure what the problem is then?
It "discovers" the resources but says the routes don't exist
routes index, edit and create
I can send screenshots later to make it easier.
when to the errors occur?
idk how it would discover resources but not register the route
Ok, the problem:
The service provider registering all resources inside each module (namespace System)
The admin panel provider
And the route error
You need to adjust the discoverResources() on the panel provider. It’s looking in a specific directory, but your “modules” are probably in a different directory. But you can also include them manually if you don’t want the auto discovery with the ->resources() modifier for the panel.
Are you talking about doing this directly in the AdminPanelProvider, inside the panel method?
Yes.
Sure, ill try this. Thanks for the suggestion
The panel is looking for resources in app\Filament\Resources but I’m sure that’s not where your modules are.
So just change the path there to be the namespace path for your modules directory.
Sure, i get it. So do I have to specify it for each panel individually?
If they are not in the same namespace as the panel then yes.
Personally we use modules too, but we don’t use auto discovery, we explicitly define the resource in each panel that needs them with ->resources()
So if I have for example:
<base_path>/modules/User;
<base_path>/modules/Factory;
<base_path>/modules/Car;
Should I use:
$panel->discoverResources("<base_path>/modules/User/resources", "namespace")
$panel->discoverResources("<base_path>/modules/Factory/resources", "namespace")
$panel->discoverResources("<base_path>/modules/User/resources", "namespace")
This?
Ohh, sure
Not quite. ->discoverResources(base_path(‘modules’)
If I’m thinking of your setup correctly.
Filament will look for any file in that “parent” directory that extends the resource class.
My setup is basically:
<BASE_PATH>/modules/<MODULE_NAME>
Resources are in: <BASE_PATH>/modules/<MODULE>/filament/<MODULE>Resource
Pages are in: <BASE_PATH>/modules/<MODULE>/filament/<MODULE>Resource/Pages
I tried to do this:
->discoverResources(in: base_path('modules'), for: 'System\\Factory\\filament\\FactoryResource')
->discoverPages(in: base_path('modules'), for: 'System\\Factory\\filament\\FactoryResource\\Pages')
But nothing happensI think you’re over complicating it with the for.
I could be wrong though.
Yeah, but needs the for
Just feels like you’re trying to force the namespace when it’s not needed
app_path(‘modules’) for ‘App\Modules’
Ohhh, working fine
->discoverResources(in: base_path('modules'), for: 'System')
Thanks bro ❤️