F
Filament6mo ago
Vitor

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:
class RegisterResourcesServiceProvider extends FilamentPluginServiceProvider
{
public static string $name = 'registerResources';

public array $resources = [];

public function register()
{
parent::register();

$folders = File::directories(base_path('modules'));
foreach ($folders as $folder) {
$folderName = basename($folder);
$resourceFiles = glob($folder . '/pages/*Resource.php');

foreach ($resourceFiles as $resourceFile) {
$resourceClassName = basename($resourceFile, '.php');
$this->resources[] = 'System\\' . $folderName . '\\pages\\' . $resourceClassName;
}
}

$this->app->resolving('filament', function () {
Filament::registerResources($this->resources);
});
}
}
class RegisterResourcesServiceProvider extends FilamentPluginServiceProvider
{
public static string $name = 'registerResources';

public array $resources = [];

public function register()
{
parent::register();

$folders = File::directories(base_path('modules'));
foreach ($folders as $folder) {
$folderName = basename($folder);
$resourceFiles = glob($folder . '/pages/*Resource.php');

foreach ($resourceFiles as $resourceFile) {
$resourceClassName = basename($resourceFile, '.php');
$this->resources[] = 'System\\' . $folderName . '\\pages\\' . $resourceClassName;
}
}

$this->app->resolving('filament', function () {
Filament::registerResources($this->resources);
});
}
}
24 Replies
Dan Harrin
Dan Harrin6mo ago
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
Vitor
VitorOP6mo ago
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
Dan Harrin
Dan Harrin6mo ago
not sure what the problem is then?
Vitor
VitorOP6mo ago
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.
Dan Harrin
Dan Harrin6mo ago
when to the errors occur? idk how it would discover resources but not register the route
Vitor
VitorOP6mo ago
Ok, the problem:
Vitor
VitorOP6mo ago
No description
Vitor
VitorOP6mo ago
The service provider registering all resources inside each module (namespace System)
Vitor
VitorOP6mo ago
The admin panel provider
No description
Vitor
VitorOP6mo ago
And the route error
No description
awcodes
awcodes6mo ago
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.
Vitor
VitorOP6mo ago
Are you talking about doing this directly in the AdminPanelProvider, inside the panel method?
awcodes
awcodes6mo ago
Yes.
Vitor
VitorOP6mo ago
Sure, ill try this. Thanks for the suggestion
awcodes
awcodes6mo ago
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.
Vitor
VitorOP6mo ago
Sure, i get it. So do I have to specify it for each panel individually?
awcodes
awcodes6mo ago
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()
Vitor
VitorOP6mo ago
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
awcodes
awcodes6mo ago
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.
Vitor
VitorOP6mo ago
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 happens
awcodes
awcodes6mo ago
I think you’re over complicating it with the for. I could be wrong though.
Vitor
VitorOP6mo ago
Yeah, but needs the for
awcodes
awcodes6mo ago
Just feels like you’re trying to force the namespace when it’s not needed app_path(‘modules’) for ‘App\Modules’
Vitor
VitorOP6mo ago
Ohhh, working fine ->discoverResources(in: base_path('modules'), for: 'System') Thanks bro ❤️
Want results from more Discord servers?
Add your server