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