How to Get Current URL, Previous URL, and Route Name in Laravel 12?

How to Get Current URL, Previous URL, and Route Name in Laravel 12 1

There are times when you may want to retrieve the current URL, the full URL with any query parameters, the previous URL, and the name of the current route in Laravel applications. This can be useful:

  • To create an active menu navigation.
  • To redirect a user back to the previous URL.
  • To track URLs of the currently viewed pages.
  • To manage conditions for routes.

Laravel 12 contains several basic methods to perform the above tasks and all those methods can be demonstrated in one example.

Laravel Development Services

Example: Get Current URL, Previous URL, and Route Name

Create a controller and add the following code:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
class PageController extends Controller
{
    public function index(Request $request)
    {
        // Get current URL
        $currentUrl = url()->current();
        // Get full URL with query parameters
        $fullUrl = url()->full();
        // Get current URL using URL facade
        $urlFacade = URL::current();
        // Get current URL using Request object
        $requestUrl = $request->url();
        // Get previous URL
        $previousUrl = url()->previous();
        // Get current route name
        $routeName = Route::currentRouteName();
        return response()->json([
            'current_url' => $currentUrl,
            'full_url' => $fullUrl,
            'url_facade' => $urlFacade,
            'request_url' => $requestUrl,
            'previous_url' => $previousUrl,
            'route_name' => $routeName,
        ]);
    }
}

Example Output

For example, suppose your current page is at:

https://example.com/products?page=2

In this case, you’d have the following output based on your current page url:

{
    "current_url": "https://example.com/products",
    "full_url": "https://example.com/products?page=2",
    "url_facade": "https://example.com/products",
    "request_url": "https://example.com/products",
    "previous_url": "https://example.com/home",
    "route_name": "products.index"
}

Method Overview

  • url()->current() – Returns the Current page URL
  • url()->full() – Returns the Full Page URL including Query Parameters
  • URL::current() – Return the Current URL using the facade
  • $request->url() – Returns the URL using the request object
  • url()->previous() – Returns the previous URL
  • Route::currentRouteName() – Returns the current Route Name

Conclusion

In Laravel 12, you have a number of methods to easily retrieve url or routing information. Use any of these methods to meet your application’s requirements.

If you would like to retrieve only the URL of the Current Page, you could call url()->current().

If you would like to retrieve a Full URL, you could call url()->full()

If you would like to retrieve the current route name, you could call Route::currentRouteName()

These methods are usually found in everyday use of Laravel routine for navigation, redirection, and routing management.

Hire Laravel Developer

FAQ

1. How do I get the current URL in Laravel 12?

Use:

url()->current();

This provides you with the current page but does not give you the query parameters.

2. How do I get the full URL including query parameters?

Use:

url()->full();

This will return the full URL, including all of your query parameters.   

3. How can I get the previous URL in Laravel?

Use:

url()->previous();

This will provide you with the URL you accessed before your current URL.  

4. How do I get the current route name in Laravel 12?

Use:

request()->route()->getName();

5. What is the difference between current() and full() in Laravel?

  • current() will return the current webpage without any of the query parameters.
  • full() will return the current page along with all of the corresponding query parameters.
Previous Article

What is Claude AI? A Guide for eCommerce, Marketing & Shopify Apps

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨