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://laravelcode.com/posts?page=3
and now see examples different methods.
\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://laravelcode.com/posts
}
\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://laravelcode.com/posts?page=3
}
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://laravelcode.com/posts
}
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://laravelcode.com/posts?page=3
}
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://laravelcode.com/posts
}
\Request::fullUrl() method will return full url.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = \Request::fullUrl();
return $full_url; // https://laravelcode.com/posts?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://laravelcode.com/posts?page=6&limit=10
}
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://laravelcode.com/posts
}
request()->fullUrl() method will return full url.
/**
* get current full url
*
* @return $this
*/
public function fullUrl()
{
$full_url = request()->fullUrl();
return $full_url; // https://laravelcode.com/posts?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://laravelcode.com/posts?page=6&limit=10
}
That way, you can get specific details from url using Laravel class. Thanks for giving time in reading article.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to remove the last element from an array in PHP
Use the PHP array_pop() functi...How to create a query string from an array in PHP
When sending get request to third party...How to check if a variable exists or defined in JavaScript
Use the typeof operator If...Some errors have been detected on the server, please look at the bottom of this window.
You might have already worked in phpMyAd...Google reCAPTCHA V3 Tutorial with Example in PHP
Google reCAPTCHA is tool by Google which...