Laravel - How to Get Current Previous URL in Controller or View

Sovary August 16, 2022 396
1 minute read

Hello today short tutorial is how to get the current URL which live on your browser in Laravel. In some case you want to retrieve the URL link from browser bar which mean current URL. In this article you will see how to get current URL in different ways by following sample example which you can apply in Laravel 6, Laravel 7, Laravel 8, Laravel 9

Laravel support helper class and URL facade which we will use to get current URL anywhere. Let's check the below examples:

Example #1 - Get Current URL

Using current() with Helper

$currentURL = url()->current();
dd($currentURL);

Example #2 - Get Current URL

Using full() with Helper (include query string)

$currentURL = url()->full();  
dd($currentURL);

Example #3 - Get Current URL

Using current() with Facade

$currentURL = URL::current();   
dd($currentURL);

Example #4 - Get Current URL

Using full() with Facade (include query string)

$currentURL = URL::full();
dd($currentURL);

Example #5 - Get Current URL

Using Request

$currentURL = Request::url();
dd($currentURL);

Example #6 - Get Current Route

$route = Route::current()->getName();
dd($route);

Example #7 - Get Previous URL

$url = url()->previous();
dd($url);

Hope these examples help you. Have a nice day!!

You might also like

 

Laravel  PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him