How to Get the Current Full URL in Laravel 12

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.

how-to-get-the-current-full-url-in-laravel-12

Prerequisites

Before we start, make sure you have:

  1. A Laravel 12 project set up.
  2. A route and controller to test the code (we’ll create these below).
  3. Basic knowledge of Laravel routing and controllers.

If you don’t have a project yet, run laravel new example-app to create one.

Step 1: Set Up a Route and Controller

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.

Step 2: Methods to Get the Current URL

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.

Method 1: Using url()->current() Helper

This 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"

 

Method 2: Using url()->full() Helper

This 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"

 

Method 3: Using URL::current() Facade

This 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"

 

Method 4: Using URL::full() Facade

This 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"

 

Method 5: Using 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().

Step 3: Get the Previous URL

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"

Step 4: Get the Current Route Name

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"

Step 5: Run the Application

To test these methods, start your Laravel server:

php artisan serve

Conclusion

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.

Frequently Asked Questions (FAQs)

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:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS