In Filament application, you may want to add buttons/links to other pages from a Filament Resource or to a custom page. How to get their URLs?
Imagine an admin panel with the path /admin
and a typical UserResource
with all pages:
- index
- create
- view
- edit
app/Filament/Resources/UserResource.php:
use App\Filament\Resources\UserResource\Pages; class UserResource extends Resource{ // ... public static function getPages(): array { return [ 'index' => Pages\ListUsers::route('/'), 'create' => Pages\CreateUser::route('/create'), 'view' => Pages\ViewUser::route('/{record}'), 'edit' => Pages\EditUser::route('/{record}/edit'), ]; }}
How would you get the link to these pages? Or what if you have a custom page that is added to the Resource or doesn't belong to any Resource?
Let's see how we can get the link for these pages, in two ways: by URL and by Route Names.
Case 1: Method getUrl() for Resource
The first way is to use the getUrl()
method on the Resource and pass the name of the page for which you want URL to be generated.
By default, the route name is set to index
. Calling this method without any parameter will return the URL for the list page like http://project.test/admin/users
.
The URL generated for each page would look like this:
Page Name | URI |
---|---|
UserResource::getUrl('index') | /admin/users |
UserResource::getUrl('create') | /admin/users/create |
UserResource::getUrl('view') | /admin/users/{record} |
UserResource::getUrl('edit') | /admin/users/{record}/edit |
Case 2: getUrl() for Custom Pages in Resource
Of course, if you add your custom page to the list, you can call it by its name. For example, here, I have added a custom page called payments
.
use App\Filament\Resources\UserResource\Pages; class UserResource extends Resource{ // ... public static function getPages(): array { return [ 'index' => Pages\ListUsers::route('/'), 'create' => Pages\CreateUser::route('/create'), 'payments' => Pages\Payments::route('/payments'), 'view' => Pages\ViewUser::route('/{record}'), 'edit' => Pages\EditUser::route('/{record}/edit'), ]; }}
If on any page I would call UserResource::getUrl('payments')
, the generated URL would be similar to this: http://project.test/admin/users/payments
.
Case 3: getUrl() for Non-Resource Custom Pages
What if you have a custom page that doesn't belong to the Resource? You can call the same getUrl()
method on the custom page. When calling this method on the custom page, you don't need to pass the page name.
For example, I have a payments
custom page by calling getUrl()
method on the page anywhere in the panel.
use App\Filament\Pages\Payments; Payments::getUrl()
It would return a similar URL to this: http://project.test/admin/payments.
By default, Filament generates complete path URLs. If you don't need that, you can set $isAbsolute
to false in the getUrl()
method.
UserResource::getUrl('payments', isAbsolute: false)
Then the URL would be /admin/users/payments
.
Getting Route Names with route()
Another way to build the links is to use the traditional Laravel route()
helper. All Filament URLs have Route names. Default Filament Route name for the Resource are generated like this:
filament.{panel_id}.resources.{resource_name}.{page_name}
For example, the route name for the list page for the UserResource
on the admin
panel would be filament.admin.resources.users.index
.
Or for the custom page:
filament.{panel_id}.pages.{slug/class_name}
For example, for the admin
panel payments
custom page without setting the slug would be: filament.admin.pages.payments
.
You can check the Route name by getting the Routes list using an Artisan command route:list
.
Warning: Multi-tenancy Parameter
When using the Filament multi-tenancy feature, you might get the error Missing parameter: tenant.
In some cases, when generating a URL with both options, you must pass the tenant as a parameter.
For the getUrl()
, the syntax would be:
UserResource::getUrl(tenant: filament()->getTenant());
This way, the current tenant would be passed as a parameter. For the route()
helper syntax would be:
route('filament.admin.resources.users.index', ['tenant' => filament()->getTenant()])
If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.
Hello, I enabled multitenancy and have 2 panels, one for adding companies and another for configuring company details, at the moment, I added an Action in the company list to redirect me to the secondary panel login, this following works just fine:
Out of curiosity, is there a better way to generate the URL?
https://laraveldaily.com/post/filament-get-urls-route-names-resources-pages