Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Request getting current path with query string

Is there a Laravel way to get the current path of a Request with its query parameters?

For instance, for the URL:

http://www.example.com/one/two?key=value

Request::getPathInfo() would return /one/two.

Request::url() would return http://www.example.com/one/two.

The desired output is /one/two?key=value.

like image 518
John Bupit Avatar asked Jul 22 '15 06:07

John Bupit


Video Answer


6 Answers

Try to use the following:

\Request::getRequestUri()
like image 95
Hubert Dziubiński Avatar answered Oct 29 '22 04:10

Hubert Dziubiński


Laravel 4.5

Just use

Request::fullUrl()

It will return the full url

You can extract the Querystring with str_replace

str_replace(Request::url(), '', Request::fullUrl())

Or you can get a array of all the queries with

Request::query()

Laravel >5.1

Just use

$request->fullUrl()

It will return the full url

You can extract the Querystring with str_replace

str_replace($request->url(), '',$request->fullUrl())

Or you can get a array of all the queries with

$request->query()
like image 23
Thomas Bolander Avatar answered Oct 29 '22 05:10

Thomas Bolander


Request class doesn't offer a method that would return exactly what you need. But you can easily get it by concatenating results of 2 other methods:

echo (Request::getPathInfo() . (Request::getQueryString() ? ('?' . Request::getQueryString()) : '');
like image 40
jedrzej.kurylo Avatar answered Oct 29 '22 06:10

jedrzej.kurylo


Get the current URL including the query string.

echo url()->full();
like image 12
Gr Brainstorm Avatar answered Oct 29 '22 04:10

Gr Brainstorm


$request->fullUrl() will also work if you are injecting Illumitate\Http\Request.

like image 3
Yada Avatar answered Oct 29 '22 04:10

Yada


If you have access to the Request $request object you can also use the non static method

$request->getRequestUri()
like image 3
Garrick Crouch Avatar answered Oct 29 '22 04:10

Garrick Crouch