使用查询字符串获取当前路径的请求

有没有一种 Laravel 的方法来获得当前的路径与查询参数的请求?

例如,对于 URL:

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

Request::getPathInfo()将返回 /one/two

Request::url()将返回 http://www.example.com/one/two

所需的输出是 /one/two?key=value

178468 次浏览

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()) : '');

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()

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

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'));
}

Try to use the following:

\Request::getRequestUri()
public functin func_name(Request $request){$reqOutput = $request->getRequestUri();}

Get the current URL including the query string.

echo url()->full();

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

$request->getRequestUri()

The simplest I found is this one:

$request->getPathInfo()