Hey there! If you’re working with Laravel 12 and need to grab the current URL or even the previous one, I’ve got you covered. In this beginner-friendly guide, I’ll show you several simple ways to get the current full URL, with or without query parameters, and even how to fetch the previous URL or current route name.
We’ll use Laravel’s built-in helpers, facades, and Request class.
Before we start, make sure you have:
If you don’t have a project yet, run laravel new example-app
to create one.
To test the URL retrieval methods, let’s create a route and a controller.
Create a Controller
Run this command to create a UserController
:
php artisan make:controller UserController
Define a Route
Open routes/web.php
and add the following route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('/test-url', [UserController::class, 'index'])->name('test.url');
This route points to the index
method in UserController
and names the route index
for later use.
Below are five easy ways to get the current URL in Laravel 12. For each method, update the app/Http/Controllers/UserController.php
file with the provided code and visit http://localhost:8000/test-url
to test it. The dd()
function will display the output.
url()->current()
HelperThis method retrieves the current URL without query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$currentURL = url()->current();
dd($currentURL);
}
}
Example Output: If you visit http://localhost:8000/test-url
, it outputs:
"http://localhost:8000/test-url"
url()->full()
HelperThis method retrieves the full URL, including query parameters (e.g., ?key=value
).
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$currentURL = url()->full();
dd($currentURL);
}
}
Example Output: For http://localhost:8000/test-url?name=John
, it outputs:
"http://localhost:8000/test-url?name=John"
URL::current()
FacadeThis is similar to Method 1 but uses the URL
facade instead of the helper.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class UserController extends Controller
{
public function index(Request $request)
{
$currentURL = URL::current();
dd($currentURL);
}
}
Example Output: Same as Method 1:
"http://localhost:8000/test-url"
URL::full()
FacadeThis retrieves the full URL with query parameters using the URL
facade.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class UserController extends Controller
{
public function index(Request $request)
{
$currentURL = URL::full();
dd($currentURL);
}
}
Example Output: Same as Method 2:
"http://localhost:8000/test-url?name=John"
Request::url()
This method uses the Request
facade to get the current URL without query parameters.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$currentURL = $request->url();
dd($currentURL);
}
}
Example Output:
"http://localhost:8000/test-url"
Note: To get the full URL with query parameters using Request
, use $request->fullUrl()
.
To retrieve the URL of the previous page (e.g., where the user came from), use the url()->previous()
helper.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$prevURL = url()->previous();
dd($prevURL);
}
}
Example Output: If you came from http://localhost:8000/home
, it outputs:
"http://localhost:8000/home"
To get the name of the current route (e.g., test.url
), use the Route
facade.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
class UserController extends Controller
{
public function index(Request $request)
{
$routeName = Route::current()->getName();
dd($routeName);
}
}
Example Output:
"test.url"
To test these methods, start your Laravel server:
php artisan serve
Retrieving the current URL in Laravel 12 is super easy with the variety of methods available! Whether you need just the base URL, the full URL with query parameters, the previous URL, or the route name, Laravel’s helpers, facades, and Request class have you covered.
I hope this guide made it simple to understand and implement these features in your project.
Q1: What’s the difference between current()
and full()
methods?
A: The current()
method returns the base URL without query parameters, while full()
includes query parameters (e.g., ?key=value
).
Q2: When should I use the Request
class vs. helpers or facades?
A: Use Request
when you’re already working with the $request
object in your controller. Helpers and facades are more concise if you don’t need other request data.
Q3: Why is url()->previous()
not working as expected?
A: The previous()
method relies on the HTTP referer header. If the user navigates directly or the referer is missing, it may return the current URL instead.
Q4: Can I get the current URL in a Blade template?
A: Yes! Use url()->current()
, url()->full()
, or request()->url()
directly in your Blade file, like {{ url()->current() }}
.
Q5: How do I handle URLs in middleware?
A: Inject the Request
object into your middleware and use $request->url()
or $request->fullUrl()
to access the current URL.
You might also like: