Laravel 10 Get Current URL Example


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. if you want to get the current page URL in laravel 10 then we can use many methods like current(), full(), request(), url().

So, let's see, how to get the current url in laravel 10, laravel 10 get the current url with parameters, get the full url in laravel 10, find the current url in laravel 9/10, get the previous url laravel 10, and how to get the current url in laravel blade file.

Example: full()

In this example, we will get the current URL including the query string.

$currentFullURL = url()->full();

echo "<br>Full URL: ".$currentFullURL;

 

 

Example: current()

In this example, we will get the current URL without the query string.

$currentURL = url()->current();

echo "<br>Current URL: ".$currentURL ;

 

Example: Get URL using Facades

In this example, we will get a URL using Facades.

use Illuminate\Support\Facades\URL;

$URLFacades = URL::current();

echo "<br>URL Facades: ".$URLFacades;

 

Example: full() with Facade and Query string parameters

Also, you can get full URLs using URL facades.

$currenturl = URL::full();

dd($currenturl);

 

Example: Get the Previous URL

In this example, we will get the full URL for the previous request.

$previousURL= url()->previous();

echo "<br>Previous URL: ".$previousURL;

 

 

Example: Get Current Route Name

In this example, we will get the current route name.

$cur_route = \Route::current()->getName();

echo "<br>Current Route: ".$cur_route;

 

Example: Get Previous Route Name

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;

 

Example: Request::is

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:

RECOMMENDED POSTS

FEATURE POSTS