在 Laravel 中更改查询参数时重定向到当前 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21650119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:00:17  来源:igfitidea点击:

Redirect to current URL while changing a query parameter in Laravel

laravellaravel-4

提问by Martti Laine

Is there a built-in way to do something like this?

有没有内置的方法来做这样的事情?

Let's say I have a search-page that has a few parameters in the URL:

假设我有一个搜索页面,其 URL 中有几个参数:

example.com/search?term=foo&type=user

A link on that page would redirect to an URL where typeis link. I'm looking for a method to do this without manually constructing the URL.

该页面上的链接将重定向到一个 URL,其中typelink。我正在寻找一种无需手动构建 URL 的方法来执行此操作。

Edit:
I could build the URL manually like so:

编辑:
我可以像这样手动构建 URL:

$qs = http_build_query(array(
    'term' => Input::get('term'),
    'type' => Input::get('type')
));
$url = URL::to('search?'.$qs);

However, what I wanted to know is if there is a nicer, built-in way of doing this in Laravel, because the code gets messier when I want to change one of those values.

但是,我想知道的是,在 Laravel 中是否有更好的内置方法来执行此操作,因为当我想更改其中一个值时,代码会变得更加混乱。

Giving the URL generator a second argument ($parameters) adds them to the URL as segments, not in the query string.

为 URL 生成器提供第二个参数 ( $parameters) 将它们作为段添加到 URL 中,而不是在查询字符串中。

回答by clarkf

You can use the URL Generator to accomplish this. Assuming that search is a named route:

您可以使用 URL Generator 来完成此操作。假设搜索是一个命名路由:

$queryToAdd = array('type' => 'user');
$currentQuery = Input::query();

// Merge our new query parameters into the current query string
$query = array_merge($queryToAdd, $currentQuery);

// Redirect to our route with the new query string
return Redirect::route('search', $query);

Laravel will take the positional parameters out of the passed array (which doesn't seem to apply to this scenario), and append the rest as a query string to the generated URL.

Laravel 将从传递的数组中取出位置参数(这似乎不适用于这种情况),并将其余部分作为查询字符串附加到生成的 URL 中。

See: URLGenerator::route(), URLGenerator::replaceRouteParameters()URLGenerator::getRouteQueryString()

见:URLGenerator::route()URLGenerator::replaceRouteParameters()URLGenerator::getRouteQueryString()

回答by Bouke Versteegh

I prefer native PHP array merging to override some parameters:

我更喜欢原生 PHP 数组合并来覆盖一些参数:

['type' => 'link'] + \Request::all()

To add or override the typeparameter and remove another the term:

要添加或覆盖type参数并删除另一个参数term

['type' => 'link'] + \Request::except('term')

Usage when generating routes:

生成路由时的用法:

route('movie::category.show', ['type' => 'link'] + \Request::all())

回答by TonyArra

You can do it with Laravel's URLGenerator

你可以使用Laravel 的 URLGenerator

URL::route('search', array(
  'term' => Input::get('term'),
  'link' => Input::get('type')
));

Edit: be sure to name the route in your routes.php file:

编辑:一定要在你的 routes.php 文件中命名路由:

Route::get('search', array('as' => 'search'));

That will work even if you're using a Route::controller()

即使您使用的是 Route::controller()

回答by J.C. Gras

From Laravel documentation:

Laravel 文档

if your route has parameters, you may pass them as the second argument to the route method.

如果你的路由有参数,你可以将它们作为第二个参数传递给路由方法。

In this case, for return an URI like example.com/search?term=foo&type=user, you can use redirect function like this:

在这种情况下,要返回像example.com/search?term=foo&type=user这样的 URI ,您可以使用如下重定向功能:

return redirect()->route('search', ['term' => 'foo', 'type' => 'user']);

回答by daylerees

The Input component should also contain query parameters.

Input 组件还应包含查询参数。

i.e Input::get('foo');

IE Input::get('foo');