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

时间:2015-07-22 06:30:42

标签: php laravel laravel-5 laravel-request

是否有Laravel方法通过查询参数获取Request的当前路径?

例如,对于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

10 个答案:

答案 0 :(得分:35)

请求类没有提供能够准确返回所需内容的方法。但是你可以通过连接其他两种方法的结果轻松获得它:

library(ggplot2)
library(caret)
library(elasticnet)
library(party)

data_set <- diamonds[1:1000, c(1, 5, 6, 7, 8, 9, 10)]
formula <- price ~ carat + depth + table + x + y + z

set.seed(100)
enet_model <- train(formula,
                    importance = TRUE,
                    data = data_set,
                    method = "enet",
                    trControl = trainControl(method = "cv"),
                    preProc = c("center", "scale"))

set.seed(100)
ctree_model <- train(formula, 
                     data = data_set,
                     method = "ctree",
                     trControl = trainControl(method = "cv"))

set.seed(Set_seed_seed)
knn_model <- train(formula,
                   importance = TRUE,
                   data = data_set,
                   method = "knn",
                   preProc = c("center", "scale"),
                   tuneGrid = data.frame(k = 1:20),
                   trControl = training_control)

varImp(enet_model)
varImp(ctree_model)
varImp(knn_model)

答案 1 :(得分:34)

Laravel 4.5

只需使用

Request::fullUrl()

它将返回完整的URL

您可以使用str_replace

提取Querystring
str_replace(Request::url(), '', Request::fullUrl())

或者您可以使用

获取所有查询的数组
Request::query()

Laravel&gt; 5.1

只需使用

$request->fullUrl()

它将返回完整的URL

您可以使用str_replace

提取Querystring
str_replace($request->url(), '',$request->fullUrl())

或者您可以使用

获取所有查询的数组
$request->query()

答案 2 :(得分:25)

尝试使用以下内容:

\Request::getRequestUri()

答案 3 :(得分:3)

如果您正在注射$request->fullUrl()

Illumitate\Http\Request也会有效。

答案 4 :(得分:1)

获取包含查询字符串的当前URL。

echo url()->full();

答案 5 :(得分:0)

从URL字符串中获取flag参数 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'));
}

答案 6 :(得分:0)

与Yada的答案类似:$ request-&gt; url()也适用于注入Illuminate \ Http \ Request

编辑:fullUrl和url之间的区别在于fullUrl包含您的查询参数

答案 7 :(得分:0)

如果您有权访问 const str = 'i need this {client.car.model} exactly at {client.order.time} today'; const data = { client: { car: { model: 'Truck' }, order: { time: '12:00PM' } } }; const getValueFromPath = (obj, path, delimiter = '.') => { // path or obj are falsy, just return obj // you could tweak this bit to do something else if you want // obj === undefined might be from our recursive call, so keep that in mind if you tweak if (!path || !obj) return obj; const parts = path.split(delimiter); const next = parts.shift(); // get the first part, parts now has all the other parts // If !parts.length (parts.length === 0), we're on the last part, so we'll return. if (!parts.length) return obj[next]; // Otherwise, recurse with the rest of the parts. return getValueFromPath(obj[next], parts.join(delimiter), delimiter); }; const result = str.replace(/{(.+?)}/gi, (_, path) => getValueFromPath(data, path)); console.log(result); 对象,您还可以使用非静态方法

Request $request

答案 8 :(得分:-1)

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

答案 9 :(得分:-1)

只需将其放到那里..... docs:https://laravel.com/docs/7.x/requests

相关问题