F
Filament14mo ago
Vincent

Removing tenant parameter and middleware for SimplePage

Hi! In the app I'm building, users can belong to multiple tenants. I'm tracking their current organization via a currentOrganization relationship on the User model. If a user's current org is empty, I'd like to redirect them to a page, /current-organization, where they can select which of their organizations they'd like to administer. I'm doing this via middleware:
// EnsureUserHasCurrentOrganization middleware
if (
! $user->current_organization_id
&& $user->organizations()->exists()
) {
return redirect(
'/current-organization',
Response::HTTP_FOUND
);
}
// EnsureUserHasCurrentOrganization middleware
if (
! $user->current_organization_id
&& $user->organizations()->exists()
) {
return redirect(
'/current-organization',
Response::HTTP_FOUND
);
}
and /current-organization:
class CurrentOrganization extends SimplePage
{
use HasRoutes;

protected static string $view = 'filament.pages.current-organization';

public static function routes(Panel $panel): void
{
$slug = static::getSlug();

Route::get("/{$slug}", static::class)
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel))
->name('current-organization');
}

public static function getWithoutRouteMiddleware(Panel $panel): string|array
{
return [
...Arr::wrap(static::$withoutRouteMiddleware),
EnsureUserHasCurrentOrganization::class,
];
}
}
class CurrentOrganization extends SimplePage
{
use HasRoutes;

protected static string $view = 'filament.pages.current-organization';

public static function routes(Panel $panel): void
{
$slug = static::getSlug();

Route::get("/{$slug}", static::class)
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel))
->name('current-organization');
}

public static function getWithoutRouteMiddleware(Panel $panel): string|array
{
return [
...Arr::wrap(static::$withoutRouteMiddleware),
EnsureUserHasCurrentOrganization::class,
];
}
}
This page is registered via the pages method in the Panel Provider. There are two problems: 1. The route above still appears in the {tenant} namespace. php artisan routes:list | grep current shows: GET|HEAD myurl.test/{tenant}/current-organization ... filament.admin.pages.current-organization › App\Filament\Pages\CurrentOrganization 2. It gets stuck in a redirect loop despite explicitly disabling the offending middleware. Is this a bug, or am I missing something? How can I remove it from the tenant routes? And how can I disable the middleware?
1 Reply
wyChoong
wyChoong14mo ago
For the middleware, can you dd to see if it runs the middleware, if it does then check your if checking You can also run artisan route:list -v to see the middleware tied to the route
Want results from more Discord servers?
Add your server