Laravel get current URL with parameters example
Sometimes you might want to find the current URL and to write condition. This way, you can add active class to current url link. You also might want to pass data with query string parameters to view instead of session in web application. This is easy way to get data in Laravel.
In this article, I will show you how to get current in Laravel using different ways. So let's see all ways:
Laravel provides URL facade class with static mathod which directly return url details. Laravel's Request class also used to get url details. These methods works on both controller as well as blade files.
Suppose I am accessing url https://hackthestuff.com/article?page=3
and now see examples different methods.
Method 1: Illuminate\Support\Facades\URL facade
\URL::current() returns the current URL without the query string.
/**
* get current url
*
* @return $this
*/
public function currentUrl()
{
$current_url = \URL::current();
return $current_url; // https://hackthestuff.com/article
}
\URL::full() returns the current URL with the query string.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = \URL::full();
return $full_url; // https://hackthestuff.com/article?page=3
}
Method 2: Global url() helper method
The same as URL facade, url() method works. url()->current() returns current URL without the query string.
/**
* get current url
*
* @return $this
*/
public function currentUrl()
{
$current_url = url()->current();
return $current_url; // https://hackthestuff.com/article
}
url()->full() returns the current URL with the query string.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = url()->full();
return $full_url; // https://hackthestuff.com/article?page=3
}
Method 3: \Request class
Note: Don't use Illuminate\Http\Request class otherwise it will through Non-static method error.
\Request::url() method will return current url.
/**
* get current url
*
* @return $this
*/
public function currentUrl()
{
$current_url = \Request::url();
return $current_url; // https://hackthestuff.com/article
}
\Request::fullUrl() method will return full url.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = \Request::fullUrl();
return $full_url; // https://hackthestuff.com/article?page=3
}
If you only want to return specific segment from the url, use \Request::segment() method. segment() require one number parameter which will return nth number of parameter.
/**
* get segment from url
*
* @return $this
*/
public function segment()
{
$segment = \Request::segment(1);
return $segment; // article
}
Suppose you want to change or add parameters to url while return, use \Request::fullUrlWithQuery() method with parameter of associative array. The array will be added as query string in url.
/**
* get current url with custom query string
*
* @return $this
*/
public function urlWithQuery()
{
$current_url = \Request::fullUrlWithQuery(['page' => '6', 'limit' => 10]);
return $current_url; // https://hackthestuff.com/article?page=6&limit=10
}
Method 4: Global request() method.
request() Global method works same as \Request class.
request()->url() method will return current url.
/**
* get current url
*
* @return $this
*/
public function currentUrl()
{
$current_url = request()->url();
return $current_url; // https://hackthestuff.com/article
}
request()->fullUrl() method will return full url.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = request()->fullUrl();
return $full_url; // https://hackthestuff.com/article?page=3
}
If you only want to return specific segment from the url, use request()->segment() method. segment() require one number parameter which will return nth number of parameter.
/**
* get segment from url
*
* @return $this
*/
public function segment()
{
$segment = request()->segment(1);
return $segment; // article
}
request()->fullUrlWithQuery() method with parameter of associative array. The array will be added as query string in url.
/**
* get current url with custom query string
*
* @return $this
*/
public function urlWithQuery()
{
$current_url = request()->fullUrlWithQuery(['page' => '6', 'limit' => 10]);
return $current_url; // https://hackthestuff.com/article?page=6&limit=10
}
That way, you can get specific details from url using Laravel class. Thanks for giving time in reading article.
Copyright 2023 HackTheStuff