diff --git a/src/Filament.php b/src/Filament.php index d3d1ca8034..0854324347 100644 --- a/src/Filament.php +++ b/src/Filament.php @@ -142,9 +142,9 @@ class Filament return $manifest[$key]; } - public function getResourceModels(string $path): Collection + public function getResourceModels(): Collection { - $models = collect(File::allFiles($path)) + $models = collect(File::allFiles(app_path())) ->map(function ($item) { $path = $item->getRelativePathName(); $class = sprintf('\%s%s', @@ -162,8 +162,10 @@ class Filament return $reflection->isSubclassOf(Model::class) && in_array(FilamentResource::class, class_implements($class)) && !$reflection->isAbstract(); + })->mapWithKeys(function ($class) { + return [class_basename($class) => $class]; }); - return $models->values(); + return $models; } } diff --git a/src/FilamentServiceProvider.php b/src/FilamentServiceProvider.php index a4ecc77e37..5b39ab71f3 100644 --- a/src/FilamentServiceProvider.php +++ b/src/FilamentServiceProvider.php @@ -118,17 +118,18 @@ class FilamentServiceProvider extends ServiceProvider { if (Features::hasResourceModels()) { $this->app->booted(function () { - $models = $this->app->filament->getResourceModels(app_path()); + $models = $this->app->filament->getResourceModels(); - $models->each(function ($class) { - $model = $this->app->make($class); - $actions = $model->actions() ?? []; + $models->each(function ($item, $key) { + $model = $this->app->make($item); + + if (array_key_exists('index', $model->actions())) { + $route = route('filament.resource', ['model' => $key]); - if (array_key_exists('index', $actions)) { Navigation::create([ - 'path' => route('filament.resource', ['model' => class_basename($model)]), - 'active' => $model->active ?? [], - 'label' => $model->label ?? Str::plural(class_basename($model)), + 'path' => $route, + 'active' => $route.'*', + 'label' => $model->label ?? Str::plural($key), 'icon' => $model->icon ?? 'heroicon-o-database', ]); } diff --git a/src/Http/Controllers/ResourceModelController.php b/src/Http/Controllers/ResourceModelController.php index 2977c823c1..5cd9eb934c 100644 --- a/src/Http/Controllers/ResourceModelController.php +++ b/src/Http/Controllers/ResourceModelController.php @@ -2,16 +2,21 @@ namespace Filament\Http\Controllers; +use Illuminate\Container\Container; +use Illuminate\Routing\Route; +use Illuminate\View\View; +use Filament; + class ResourceModelController extends Controller { - /** - * Show the resource for the given model. - * - * @param int $id - * @return \Illuminate\View\View - */ - public function __invoke(string $model, string $action = 'index') + public function __invoke(Container $container, Route $route, string $model, string $action = 'index'): View { - return 'Model: '.$model.' action: '.$action; + $model = Filament::getResourceModels()->get($model); + abort_unless($model, 404); + + $component = collect(app($model)->actions())->get($action); + abort_unless($component, 400, __("`$action` is not a valid resource action in `$model`")); + + return call_user_func((new $component), $container, $route); } } \ No newline at end of file