Getting current url in Filament

Why isn't {{ url()->current() }} in Filament views showing subpage urls in spa mode? I need to get the current url but {{ url()->current() }} will show http://example.com/admin when in Filament. It show always just the root url and not the subpages. In frontend that is using full component Livewire navigation the same code it will show: http://example.com/contact or http://example.com/articles/45 etc. How to get the current Filament's url I am on? Is there any global variable with this info across the whole Filament panel so I don't even need to be dependent of url()->current()?
10 Replies
Wirkhof
Wirkhof4mo ago
Is there a globally accessible variable in Filament with the current url I am on? When I do a Google search for "fillament url variable of current page" there is absolutely nothing relevant. This should be absolutely easy to do but no search results? If there is no variable like that, can I use javascript inside livewire component? Something like window.location.href? I always get http://example.com/livewire/update as the path if I query it from the livewire component no matter what 😦 or the root like http://example.com/admin for everything if I call it from a component. I guess it's the same because it is not refreshing on page changes when navigating. Is there a way to refresh a component on each page change so it will contain the correct url all the time? If I try to add something like this in a custom footer.blade.php component in Filament:
@php
$url = url()->current();
@endphp
{{ $url }}
@php
$url = url()->current();
@endphp
{{ $url }}
I will get the correct path in spa() mode only when I do brewser refresh, not when navigating on the page with mouse. How to make this code dynamic and reflect the real page url all the time? Is there some Livewire/Filament method or hack how to force this value to be changed on each navigation in ->spa() mode? I have tried:
->renderHook(
'panels::body.end',
fn () => view('filament.components.footer', ["url" => url()->current()]),
)
->renderHook(
'panels::body.end',
fn () => view('filament.components.footer', ["url" => url()->current()]),
)
and then in footer.blade.php
{{ $url }}
{{ $url }}
but with no avail. The url is not refreshed while navigating in Filament backend. It only shows the correct value the first time I land in admin, or when I do hard refresh in browser. How do I make the url value dynamic in Filament blade files?
Hasnayeen
Hasnayeen4mo ago
try with {{ $this->getUrl() }}
Wirkhof
Wirkhof4mo ago
I am getting: Using $this when not in object context if I add {{ $this->getUrl() }} in footer.blade.php
Hasnayeen
Hasnayeen4mo ago
not in your footer, footer isn't a page, rather in your sub page component where you want to see it
Wirkhof
Wirkhof4mo ago
No, I need it in footer specifically. Do I really need some javascript workaround to grab the current URL value from browser? Is it really so difficult? This should be something basic, I at least thought.
Wirkhof
Wirkhof4mo ago
I think I need to listen to event wire:navigate and rerender the component. What is the name of even that is dispatched when you do wire:navigate in Filament/Livewire? And then do something like
#[On('wire-navigate-or-whatever)]
public function refreshthiscomponent()
{
//
}
#[On('wire-navigate-or-whatever)]
public function refreshthiscomponent()
{
//
}
If wire:navigate in Filament emits nothing, then I am screwed, right? Do you know the name of the event I have to put in On attribute? I think there is a magic method like $refresh or something, can it be used in this context somehow? Perhaps even in the footer.blade.php file diretly? Do I need to use JavaScript and this:
livewire:navigated
livewire:navigated
awcodes
awcodes4mo ago
Have you tried the request() helper instead of url()?
Wirkhof
Wirkhof4mo ago
I have solved it via Alpine in the end.
Sawal
Sawal2mo ago
how?
Want results from more Discord servers?
Add your server
More Posts