Laravel Request getting current path with query string
Asked Answered
W

10

101

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.

Whip answered 22/7, 2015 at 6:30 Comment(0)
M
130

Try to use the following:

\Request::getRequestUri()
Microphotograph answered 20/9, 2016 at 9:49 Comment(3)
This should be the accepted answer. It returns exactly what OP wants. Works fine on 5.2.Policeman
Still working on Laravel 9. Alternative syntax using the helper: request()->getRequestUri().Elfie
Would this work to just get that one value from the key param? request()->get('key')Staminody
K
67

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()
Kissie answered 1/2, 2016 at 12:41 Comment(4)
in newer versions of laravel use $request instance instead of statically calling the function of RequestSacroiliac
in my case, I wanted the query parameter order to be preserved, but fullUrl mysteriously changes the order of my parameter. i had to resort in pure php then $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";Sacroiliac
Request::fullUrl() worked perfectly on Laravel 4.Coaming
$request->query() awesome in 5.4Entrust
E
41

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()) : '');
Emera answered 22/7, 2015 at 7:58 Comment(3)
Yeah, there are tons of ways otherwise: str_replace(url(), '', Request::fullUrl())Whip
request()->getQueryString() has the added benefit of being available in the blade without any odd include's or variables being passed in if you need to alter url's in the content.Hanafee
Actually there is a method that returns exactly the full URL... The answer below works in Laravel 5 alsoCamm
V
16

Get the current URL including the query string.

echo url()->full();
Vexillum answered 3/3, 2019 at 19:7 Comment(1)
if you'll have dots in query they will become underscores e.g. nested.foo=bar => nested_foo=bar, \Request::getRequestUri() gives exact resultsTriangulation
O
5

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

$request->getRequestUri()
Overshadow answered 18/5, 2021 at 12:36 Comment(0)
Z
2

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

Zachar answered 16/3, 2016 at 20:23 Comment(0)
C
1

The simplest I found is this one:

$request->getPathInfo()
Cavorelievo answered 20/1, 2022 at 12:7 Comment(1)
The question was about getting the full path including the query string. The question already mentions that the getPathInfo() method did not work for this.Talos
N
1

This worked in laravel 9 for me:

$request()->getRequestUri()
Nason answered 17/4, 2023 at 14:36 Comment(1)
Hi, there is already this answer. Also, you are missing $Clearing
D
0
public functin func_name(Request $request){$reqOutput = $request->getRequestUri();}
Dabbs answered 6/12, 2016 at 5:34 Comment(0)
S
-1

Get the flag parameter from the URL string http://cube.wisercapital.com/hf/create?flag=1

public function create(Request $request)
{
$flag = $request->input('flag');
return view('hf.create', compact('page_title', 'page_description', 'flag'));
}
Search answered 7/7, 2016 at 14:39 Comment(1)
This doesn't answer the question.Nd

© 2022 - 2024 — McMap. All rights reserved.