In this article, we will see laravel 10 get the current URL example. Here, we will learn about how to get the current URL in laravel 10 and laravel 11. if you want to get the current page URL in laravel 10 then we can use many methods like current(), full(), request(), url().
Also, you can get the current URL and the previous URL in laravel 11.
So, let's see, how to get the current URL in laravel 10 and laravel 11, laravel 11 gets the current URL with parameters, and get the full URL in laravel 10.
In this example, we will get the current URL including the query string.
$currentFullURL = url()->full();
echo "<br>Full URL: ".$currentFullURL;
In this example, we will get the current URL without the query string.
$currentURL = url()->current();
echo "<br>Current URL: ".$currentURL ;
In this example, we will get a URL using Facades.
use Illuminate\Support\Facades\URL;
$URLFacades = URL::current();
echo "<br>URL Facades: ".$URLFacades;
Also, you can get full URLs using URL facades.
$currenturl = URL::full();
dd($currenturl);
In this example, we will get the full URL for the previous request.
$previousURL= url()->previous();
echo "<br>Previous URL: ".$previousURL;
In this example, we will get the current route name.
$cur_route = \Route::current()->getName();
echo "<br>Current Route: ".$cur_route;
In this example, we will get the previous route name.
$previous_Route_Name = app('router')->getRoutes()->match(app('request')->create(URL::previous()))->getName();
echo "<br>Previous Route Name: ".$previous_Route_Name;
In this example, you can check the request path.
$request_example = \Request::is('index') ? 'index path' : 'other path';
echo "<br>Request is: ".$request_example;
// For Blade File
<li class="{{ Request::is('admin/dashboard') ? 'active' : '' }}">Dashboard</li>
Output:
http://localhost:8000/index/1
Full URL: http://localhost:8000/index?mail=test%40gmail.com
Current URL: http://localhost:8000/index
URL Facades: http://localhost:8000/index
Previous URL: http://localhost:8000/index?mail=test%40gmail.com
Current Route Name: index
Previous Route Name: index
Request is: index path
You might also like: