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

Sovary August 16, 2022 614
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him