93

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.

John Bupit
  • 10,406
  • 8
  • 39
  • 75

10 Answers10

125

Try to use the following:

\Request::getRequestUri()
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Hubert Dziubiński
  • 1,386
  • 1
  • 11
  • 5
65

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()
Thomas Bolander
  • 3,892
  • 3
  • 22
  • 30
  • in newer versions of laravel use `$request` instance instead of statically calling the function of Request – DivineCoder Aug 31 '17 at 10:22
  • 1
    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]"; – DivineCoder Aug 31 '17 at 10:46
  • 1
    `Request::fullUrl()` worked perfectly on Laravel 4. – nensamuel Apr 15 '19 at 12:27
  • 2
    `$request->query()` awesome in 5.4 – Paranoid Android Dec 10 '19 at 14:22
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()) : '');
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • 3
    Yeah, there are tons of ways otherwise: `str_replace(url(), '', Request::fullUrl())` – John Bupit Jul 22 '15 at 08:15
  • 3
    `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. – Spencer O'Reilly May 12 '17 at 16:19
  • Actually there is a method that returns exactly the full URL... The [answer below](https://stackoverflow.com/a/35130810/1090395) works in Laravel 5 also – Mladen Janjetovic Sep 06 '18 at 09:11
13

Get the current URL including the query string.

echo url()->full();
Gr Brainstorm
  • 149
  • 2
  • 2
  • 2
    if you'll have dots in query they will become underscores e.g. `nested.foo=bar` => `nested_foo=bar`, `\Request::getRequestUri()` gives exact results – Lizard Derad Apr 09 '21 at 08:44
5

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

$request->getRequestUri()
Garrick Crouch
  • 309
  • 4
  • 11
2

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

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Yada
  • 30,349
  • 24
  • 103
  • 144
0
public functin func_name(Request $request){$reqOutput = $request->getRequestUri();}
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Indu
  • 53
  • 8
0

The simplest I found is this one:

$request->getPathInfo()
  • 2
    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. – miken32 Jan 21 '22 at 00:17
0

This worked in laravel 9 for me:

$request()->getRequestUri()
Slin-b
  • 1
  • 2
  • 1
    Hi, there is already this [answer](https://stackoverflow.com/a/67586325/7353417). Also, you are missing `$` – pierpy Apr 19 '23 at 19:49
-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'));
}
Ajai
  • 2,492
  • 1
  • 14
  • 23