ralphjsmit / livewire-urls
Get the previous and current url in Livewire.
Installs: 111 955
Dependents: 1
Suggesters: 0
Security: 0
Stars: 74
Watchers: 1
Forks: 9
Open Issues: 6
Requires
- php: ^8.0
- illuminate/contracts: ^8.83|^9.0|^10.0|^11.0
- livewire/livewire: ^2.10|^3.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nesbot/carbon: ^2.66|^3.0
- nunomaduro/collision: ^5.0|^6.0|^7.0|^8.0
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.21|^2.34
- pestphp/pest-plugin-laravel: ^1.1|^2.3
- phpunit/phpunit: ^9.5|^10.0
- spatie/laravel-ray: ^1.26
README
Get the current and previous url in Livewire.
This package 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 a 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 \RalphJSmit\Livewire\Urls\Middleware\LivewireUrlsMiddleware::class,
to your Http Kernel.php
, in 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.
For example:
- User visits page A
- User visits page B
- User visits page B
The Url::lastRecorded()
would give you the url of page A. The function returns null
when there isn't an other 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 an other url found, apart from the current session.
General
🐞 If you spot a bug, please submit a detailed issue and I'll try to fix it as soon as possible.
🔐 If you discover a vulnerability, please review our security policy.
🙌 If you want to contribute, please submit a pull request. All PRs will be fully credited. If you're unsure whether I'd accept your idea, feel free to contact me!
🙋♂️ Ralph J. Smit