Home » Laravel Get Current Url Extensive Tutorial

Laravel Get Current Url Extensive Tutorial

Loved it? Please Share on Social Media

Introduction:

Sometimes, your application needs to check the current URL, So in this tutorial, I will talk about how laravel get current URL at your application controller level. 

Now, you might have a question about when we need to check the current URL of our application. Well, some applications need to track/check user activity/events or the exact location; that’s why we need this type of feature in that application.

Hey, What’s up, everybody? I hope you are doing fine. Today, we will discuss the laravel get current URL procedure. You can check your current URL in your Laravel application, which is easy.

So, in this tutorial, we’ll learn how to integrate laravel get current url into our application and experiment with various code snippets.

Getting Our Project Ready for Laravel Get Current Url Feature:

First, we will prepare our project by downloading Laravel from the internet and configuring our database.

Getting Current URL using Request/URL Facade:

Well, we can get our application’s current URL using Request Facade. Please follow the code below for more details.

use Illuminate\Support\Facades\Request;
$your_current_url_01 = Request::url();
print_r($your_current_url_01); //returns the current url

use Illuminate\Support\Facades\URL;
$your_current_url_02 = URL::current();
print_r($your_current_url_02); //returns the current url

use Illuminate\Support\Facades\URL;
$your_current_url_03 = URL::full();
print_r($your_current_url_03); //returns the full current url

Getting current URL Using The Global Helper Function:

$your_current_url = request()->current();
print_r($your_current_Url);

$your_current_url = url()->current();
print_r($your_current_Url);

$your_current_url = url()->full();
print_r($your_current_Url);

Tips Relevant to Laravel Get Current URL Procedure:

You can also get other URL parts using the code snippets below.

$route = url()->currentPath();
print_r($route); // you will only get the route.

$queryStringURL = url()->getQueryString();
print_r($queryStringURL); // you will only get the query strings of your url

Last Words for Laravel Get Current URL Tutorial:

Above, I’ve discussed enough about Laravel get current URL procedures. Now it’s your turn to practice all those snippets in your code editor. I hope that you will learn a lot of things from this post. If you want to learn more about this topic, please check out their documentation.

Conclusion:

Finished! That’s all about our tutorial. Although this tutorial cannot make you an expert in the laravel get current URL procedure, it will give you some fundamental knowledge of how to work with this in the future. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.