How to Get Current Full URL in Laravel 12?
In this short article, I will show you how to get current full url in laravel 12 application.
Laravel 12 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 12 Ajax CRUD Operation Tutorial
- Laravel 12 Create Custom Error Page Example
- Laravel 12 JQuery UI Ajax Autocomplete Search
- Laravel 12 Model Events Example Tutorial
- How to Get Last Executed Query in Laravel 12?
- Laravel 12 Flash Message Example Example
- Laravel 12 Change Date Format Examples
- Laravel 12 Send Email using Queue Example
- Laravel 12 Multiple Image Upload Example
- Laravel 12 Multiple File Upload Example
- Laravel 12 Database Seeder Tutorial Example
- Laravel 12 Bootstrap Auth Scaffolding Tutorial