How to Get Current Full URL in Laravel 11?

By Hardik Savani April 16, 2024 Category : Laravel

In this short article, I will show you how to get current full url in laravel 11 application.

Laravel 11 provides several methods to get the current URL. We will use the URL facade, url() helper, Route facade, and Request facade to get the current URL. So, let's see one-by-one example below:

laravel 11 get full url example

Get Current URL in Laravel:

Example 1: current() with Helper

we can use current() function with url() helper to get current url:

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $currentURL = url()->current();
  
        dd($currentURL);
    }
}

Example 2: full() with Helper(with query string parameters)

we can use full() function with url() helper to get current full url with query string parameters:

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $currentURL = url()->full();
  
        dd($currentURL);
    }
}

Example 3: current() with Facade

we can use current() function with URL facade to get current url:

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $currentURL = URL::current();
  
        dd($currentURL);
    }
}

Example 4: full() with Facade(with query string parameters)

we can use full() function with URL facade to get current full url with query string parameters:

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $currentURL = URL::full();
  
        dd($currentURL);
    }
}

Example 5: using Request

we can use url() function with Request facade to get current url:

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $currentURL = Request::url();
  
        dd($currentURL);
    }
}

Get Previous URL in Laravel:

we can use previous() method with url() helper for getting previous url.

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $prevURL = url()->previous();
  
        dd($prevURL);
    }
}

Get Current Route in Laravel:

we can use getName() function to get current route name using Route facade.

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
    
class UserController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function index(Request $request)
    {
        $routeName = Route::current()->getName();
  
        dd($routeName);
    }
}
$route = ;
  
dd($route);

I hope it can help you....

Shares