Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix pathless panels error pages #3

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/FilamentErrorPagesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
);
}
}
}
Expand Down