In this tutorial, you will learn how to get current full URL in laravel 12, previous full URL, with or without query parameters, and even fetch the current route name. We’ll use both helper functions and facade methods to demonstrate each approach clearly. You’ll find working examples for each method, including how to access these URLs inside your Blade templates.
Whether you’re building redirects, tracking page visits, or debugging route flows, knowing how to manage URLs is essential—and this guide makes it simple for Laravel beginners and pros alike.
What You’ll Need Before Getting Started
Before diving in, ensure the following are in place:
- A working Laravel 12 project already installed.
- A basic understanding of how routes and controllers work in Laravel.
- At least one route available for testing in web.php and one method inside the controller
Install Laravel Project, Create Controller & Define Routes
This step is not required if you have already installed the Laravel project; you can skip it. Let’s set up a fresh Laravel 12 application. Laravel uses Composer to handle project creation and dependencies.
Open your terminal and run the following command:
composer create-project laravel/laravel laravel-demo-app
This command will generate a brand-new Laravel 12 project inside a folder named laravel-demo-app, pulling in all the necessary files and dependencies.
Once the setup is complete, navigate to your new project directory:
cd laravel-demo-app
Create Controller
Learn how to retrieve URLs using various methods in Laravel by first setting up a dedicated controller.
Use the following Artisan command to generate a new controller named DemoController
:
php artisan make:controller DemoController
This command will create the controller file in the app/Http/Controllers
directory, where we’ll define our logic for fetching URLs.
Define a Route
Open the routes/web.php and insert the routes shown below in your web.php file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
Route::get('/url-example', [DemoController::class,'getUrl'])->name('url.example');
Step 1: Methods to Get the Current URL
In this post, we’ll explore eight different methods to retrieve the current full URL in Laravel. Additionally, we’ll look at one method to get the previous URL and another method to fetch the full URL using a route name.
For demonstration purposes, we’ll use the testing URL http://127.0.0.1:8000/url-example and display the output using the dd() function.
Method 1: Using current() Helper
Use this function to get the current URL without the query string. For example, if the URL is http://127.0.0.1:8000/url-example?name=test
, it will return http://127.0.0.1:8000/url-example
by removing the query string.
Open App/Http/Controllers/DemoController.php, add following code in your controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl()
{
$current_url = url()->current();
dd($current_url);
}
}
/** */
Open your browser and type the url “http://127.0.0.1:8000/url-example” ,Press Enter, and you will see the following output:
"http://127.0.0.1:8000/url-example"
Method 2: Using request()->url() helper
This method request()->url() return the url without any query parameters.If the input URL is http://127.0.0.1:8000/url-example?name=test
, the result will be:http://127.0.0.1:8000/url-example
.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl()
{
$current_url = request()->url();
dd($current_url);
}
}
Input url is http://127.0.0.1:8000/url-example
,the output is :
http://127.0.0.1:8000/url-example
Method 3:Using request()->fullUrl() Helper
Use this function to get the current URL with the query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl()
{
$current_url = request()->fullUrl();
dd($current_url);
}
}
Type this url http://127.0.0.1:8000/url-example?name=test
on your browser the output is:
http://127.0.0.1:8000/url-example?name=test
Method 4: Using URL::current() Facade
Use URL::current() to get the current URL without the query string. For example, if the URL is http://127.0.0.1:8000/url-example?name=test
, then the output is http://127.0.0.1:8000/url-example
without the query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl()
{
$current_url = URL::current();
dd($current_url);
}
}
Input url is http://127.0.0.1:8000/url-example?name=test
, and the output is :
http://127.0.0.1:8000/url-example
Method 5: Using URL::full() Facade
Its output is the same as url()->full(), which returns the current URL along with any query parameters, if they exist.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl()
{
$current_url = URL::full();
dd($current_url);
}
}
Given the input URL http://127.0.0.1:8000/url-example?name=test, the output will be:
"http://127.0.0.1:8000/url-example?name=test"
Method 6: Using $request->url()
This method is similar to url()->current() and URL::current().When the input URL is with query parameters ,it will returns only url without query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl(Request $request)
{
$current_url = $request->url();
dd($current_url);
}
}
Url in Browser http://127.0.0.1:8000/url-example?name=test, the output is:
http://127.0.0.1:8000/url-example
Method 7: Using $request->fullUrl()
This method is similar to $request->url().You will get the full url with query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl(Request $request)
{
$current_url = $request->fullUrl();
dd($current_url);
}
}
Url in Browser http://127.0.0.1:8000/url-example?name=test, the output is:
http://127.0.0.1:8000/url-example?name=test
Get The Previous URL Using url()->previous()
This method returns the previous url.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class DemoController extends Controller
{
public function getUrl(Request $request)
{
$previous_url = url()->previous();
dd($previous_url);
}
}
Url in Browser http://127.0.0.1:8000/url-example, if you came from welcome page the output is:
http://127.0.0.1:8000/welcome
Get the Current Route Name Using Route Facade
Here you will see how to get the current route name using route Facade
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
class DemoController extends Controller
{
public function getUrl(Request $request)
{
$currentRouteName = Route::currentRouteName();
dd($currentRouteName);
}
}
Url in Browser http://127.0.0.1:8000/url-example, the output is :
"url.example"
Conclsion
In this tutorial, we explored multiple ways to retrieve the current URL, previous URL, and current route name in Laravel 12. Whether you’re using helper functions like url()
and request()
,$request, or facades like URL
and Route
, Laravel provides flexible options to access important routing information.
You can also use these methods directly in your Blade files—for example:{{ url()->current() }}
, {{ url()->full() }}
, or {{ request()->url() }}
—to fetch and display the current URL as needed.
<p class="mb-1 font-medium">{{request()->url()}}</p>