Livewire Urls is an amazing package that gives you a simple way to retrieve the current and previous URL in Livewire.
Unfortunately, Laravel or Livewire cannot handle this for you, since Livewire also makes requests to your server when a user interacts with your webpage. This means that the usual methods like URL()->current() point to an internal Livewire route, instead of the "real route" your user is on. This package provides you with middleware and helper methods to determine which URL is currently being used.
You can install the package via composer:
composer require ralphjsmit/livewire-urls
Next, you should add the
LivewireUrlsMiddleware to your HTTP
Kernel.php. You should add it to the web key of the
use RalphJSmit\Livewire\Urls\Facades\Url; $currentUrl = Url::current();
$currentRouteName = Url::currentRoute();
Url::currentRoute() returns null when the user is on a route without a name.
$previousUrl = Url::previous();
Url::previous() method returns null when there isn't a previous route available.
$previousRouteName = Url::previousRoute();
Url::previousRoute() returns null when there isn't a previous route or if the previous route wasn't a named route.
Last recorded url
You can use the
Url::lastRecorded() method to get the last url from the history that is different from the current url.
Url::lastRecorded() would give you the URL of page A. The function returns null when there isn't another URL found, apart from the current session.
$lastRecordedUrl = Url::lastRecorded();
Last recorded route
You can use the
Url::lastRecordedRoute() method to get the last route from the history that is different from the current
$lastRecordedRoute = Url::lastRecordedRoute();
Url::lastRecordedRoute() would give you the route of page A from the previous example if page A is on a named route. Otherwise, it would return null. The function also returns null when there isn't another URL found, apart from the current session.
For more details, visit here.
If you have a stunning idea then Codebrisk is always here to convert your ideas into reality. Our team of expert developers is trained in Laravel custom web app development, CRM software development, and e-commerce app development. We run with a superior level of agility and a vast Laravel ecosystem to customize the software for your business. For more details, please contact us or you can launch a project with us.
Published at : 03-08-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.