Blog Detail

03

Aug
Getting the Current and Previous URLs in Laravel Livewire cover image

arrow_back Getting the Current and Previous URLs in Laravel Livewire

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.

Installation

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 $middlewareGroups property.

Usage

Current URL

use RalphJSmit\Livewire\Urls\Facades\Url;

$currentUrl = Url::current();

Current route

$currentRouteName = Url::currentRoute();

The Url::currentRoute() returns null when the user is on a route without a name.

Previous url

$previousUrl = Url::previous();

The Url::previous() method returns null when there isn’t a previous route available.

Previous route

$previousRouteName = Url::previousRoute();

The 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.

The 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 url/route.

$lastRecordedRoute = Url::lastRecordedRoute();

The 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.

Closing Notes

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

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

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.

Launch your project

Launch project