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:
Using current() with Helper
$currentURL = url()->current();
dd($currentURL);
Using full() with Helper (include query string)
$currentURL = url()->full();
dd($currentURL);
Using current() with Facade
$currentURL = URL::current();
dd($currentURL);
Using full() with Facade (include query string)
$currentURL = URL::full();
dd($currentURL);
Using Request
$currentURL = Request::url();
dd($currentURL);
$route = Route::current()->getName();
dd($route);
$url = url()->previous();
dd($url);
Hope these examples help you. Have a nice day!!
You might also like
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