How to Get Current Full URL in Laravel 11?
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:
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....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Resize Image Before Upload Example
- Laravel 11 Model Events Example Tutorial
- Laravel 11 Multi Auth: Create Multiple Authentication in Laravel 11
- Laravel 11 Resource Route and Controller Example
- Laravel 11 Scout Full Text Search Tutorial
- Laravel 11 JQuery UI Ajax Autocomplete Search Example
- Laravel 11 Ajax CRUD Operation Tutorial Example
- Laravel 11 Send Email using Queue Example
- Laravel 11 Yajra Datatables Example Tutorial
- Laravel 11 REST API Authentication using Sanctum Tutorial
- Laravel 11 Ajax Request Example Tutorial
- Laravel 11 Ajax Form Validation Example Tutorial
- Laravel 11 Import Export Excel and CSV File Tutorial
- Laravel 11 Form Validation Example Tutorial
- Laravel 11 CRUD Application Example Tutorial