diff --git a/src/FilamentErrorPagesServiceProvider.php b/src/FilamentErrorPagesServiceProvider.php index 0d5439a..567353d 100644 --- a/src/FilamentErrorPagesServiceProvider.php +++ b/src/FilamentErrorPagesServiceProvider.php @@ -2,13 +2,13 @@ namespace Cmsmaxinc\FilamentErrorPages; +use Filament\Panel; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Routing\Redirector; use Illuminate\Support\Facades\App; use Spatie\LaravelPackageTools\Commands\InstallCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; class FilamentErrorPagesServiceProvider extends PackageServiceProvider @@ -73,8 +73,18 @@ protected function registerCustomErrorHandler(): void $panelName = $path->before('/')->value(); $tenantId = $path->match('/\d+/')->value(); + $panels = filament()->getPanels(); + $currentPanel = $panels[$panelName] ?? false; + + /* + * If the current panel is not found, we're using the default panel. + * Some people might have a pathless panel, so we're using the default panel in that case. + * If the pathless panel is not the default panel it will still show the default Laravel error page. + */ + $panel = $currentPanel ?: filament()->getDefaultPanel(); + // Set the current panel if it exists in the available panels - if ($panel = filament()->getPanels()[$panelName] ?? false) { + if ($panel) { filament()->setCurrentPanel($panel); // Get the plugins of the current panel @@ -92,17 +102,13 @@ protected function registerCustomErrorHandler(): void filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null ); - // Handle NotFoundHttpException for panels + // If the previous request was not redirected to the error page, redirect to the error page if (! $isRedirected) { - $isDefaultPanel = filament()->getCurrentPanel()->getId() === filament()->getDefaultPanel()->getId(); - - if (filament()->getPanels()[$panelName] ?? $isDefaultPanel) { - // https://github.com/livewire/livewire/discussions/4905#discussioncomment-7115155 - return (new Redirector(App::get('url')))->route( - $route, - filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null - ); - } + // https://github.com/livewire/livewire/discussions/4905#discussioncomment-7115155 + return (new Redirector(App::get('url')))->route( + $route, + filament()->getCurrentPanel()->getTenantModel() ? $tenantId : null + ); } } }