Ralph J. Smit Laravel Software Engineer
Laravel provides a nice and easy way to get the current url: url()->current()
. But have you ever tried using this within Livewire? If so, you won't be surprised that it looks something like /livewire/message
. Why is that?
Most of you probably know that Livewire works by sending AJAX requests from the frontend to the backend, to Laravel. The url it uses for that is /livewire/message
. Thus, for each interaction or request, Laravel thinks that there was actually a new URL visit, to that /livewire/message
url. It's logical then that url()->current()
won't work here.
So, how do people actually solve this? A common way was to store the current url in a property from the mount()
method. However, that didn't work flawlessly.
In order to solve this problem, I created a new package [ralphjsmit/livewire-urls](https://github.com/ralphjsmit/livewire-urls)
.
Under the hood, this package works by adding a simple middleware, that stores the current and previous URL and route in the session.
Getting the current and previous URL in Laravel Livewire
First, install the package:
composer require ralphjsmit/livewire-urls
Next, you should open up your Http Kernel and add the RalphJSmit\Livewire\Urls\Middleware\LivewireUrlsMiddleware
to your web
route group.
Get the current URL and current route in Livewire
Usage is very simple. To get the current url and the route (if there is a route, otherwise null
), you can call the Url::current()
and Url::currentRoute()
method on the special facade:
use RalphJSmit\Livewire\Urls\Facades\Url; $currentUrl = Url::current(); $currentRouteName = Url::currentRoute();
Please make sure that you are using the
RalphJSmit\Livewire\Urls\Facades\Url
facade and not the defaultIlluminate\Support\Facades\URL
facade.
Get the previous URL and previous route in Livewire
In order to get the previous url and previous corresponding route, you may use the Url::previous()
and Url::previousRoute()
methods:
$previousUrl = Url::previous(); $previousRouteName = Url::previousRoute();
Getting the last different url
There's also a handy feature that allows you to get the last different url the user visits.
Consider the following visits:
- User visits page A
- User visits page B
- User visits page B
In this case, you don't want to redirect the user again to the url in #2, which is still the same url as current. In order to redirect them back to the last different url, you can use the Url::lastRecorded()
and Url::lastRecordedRoute()
methods:
$lastRecordedUrl = Url::lastRecorded(); $lastRecordedRoute = Url::lastRecordedRoute();
Please be aware that it will return null
if there wasn't a last different url. You can always provide a fallback url or route, by passing it as a parameter to any one of these methods.
Conclusion
I initially created this package to use myself, but I think that it can be very useful to other developers as well. At least, I've had to deal with this question several times already, so it's certainly not uncommon. It would be even better if Livewire itself offered a solution for that, perhaps in the upcoming V3.
I’d highly appreciate if if you could spare a minute and star the repo on GitHub✨.
Feel free to open an issue or leave a comment with your ideas and questions!
Published by Ralph J. Smit on in Livewire .