如何使用 Laravel 的 url 生成器助手生成一个 url,其中参数不在最后?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34636572/
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
How to generate a url with Laravel's url generator helper with parameters that are not at the end?
提问by Chris
Normally it is very simple to add parameters using the Laravel url
helper method -- as simple as:
通常使用 Laravelurl
辅助方法添加参数非常简单——就像这样简单:
url("foo/show/", [333]); // site.com/foo/show/333
However, how do you do it when parameters are at a midpoint, such as:
但是,当参数处于中点时如何进行,例如:
url("foo/?/bar/?/show", [333,444]);
I have tried the following which all fail (some come close though):
我已经尝试了以下都失败了(虽然有些接近):
url("foo/?/bar/?/show", [333,444]);
// site.com/foo//333/444?/bar/?/show
url("foo/{a}/bar/{b}/show", [333,444]);
// site.com/foo/{a}/bar/{b}/show/333/444
url("foo/{a}/bar/{b}/show", ['a' => 333, 'b' => 444]);
// site.com/foo/{a}/bar/{b}/show/333/444
The ?
is obviously important, because it gets close to the result...
该?
显然是重要的,因为它得到接近的结果...
Note
笔记
I am specifically talking about unnamed routes. So the route
function is not in question. Named routes aren't possible in the current project, so I need to reiterate I am looking for the use of the url
method.
我特别在谈论未命名的路线。所以route
功能没有问题。命名路由在当前项目中是不可能的,所以我需要重申我正在寻找该url
方法的使用。
I 100% agree that normally named routes are the way to go.
我 100% 同意通常命名的路线是要走的路。
回答by Firat Akandere
Try this:
尝试这个:
url(sprintf("foo/%d/bar/%d/show", 333, 444));
回答by Rajender Joshi
I prefer this approach rather than hardcoding url on different pages.
我更喜欢这种方法,而不是在不同页面上硬编码 url。
Route::get('foo/{param1}/bar/{param2}/show', array('as' => 'someRoute', 'uses' => 'SomeController@someMethod'))
->where(array(
'param1' => '[a-zA-Z0-9-]+',
'param2' => '[a-zA-Z0-9-]+'
));
Controller:
控制器:
public function someMethod($param1, $param2)
{
return $param1 . $param2;
}
// Assuming you passed params in controller
// 假设你在控制器中传递了参数
{{ route('someRoute') }}
// If not
// 如果不
{{ route('someRoute', ['param1' => 'test', 'param2' => 'routing']) }}
回答by Nik Sumeiko
If parameters are not GET
request parameters, but parts of the path at a midpoint (like exactly asked in a question), it is possible to generate url using Laravel's url()
helperwithout using sprintf()
at all:
如果参数不是GET
请求参数,而是路径中点的一部分(就像问题中所问的那样),则可以使用Laravel 的url()
帮助程序生成 url,而根本不使用sprintf()
:
url('foo', [333, 'bar', 444, 'show']);
// site.com/foo/333/bar/444/show
However, as pointed in other answers – named routesare preferable.
但是,正如其他答案中所指出的那样 -命名路由更可取。
回答by user1669496
If you name your routes, you can use the route
function.
如果您为路线命名,则可以使用该route
功能。
Route::get('some/{test}/{a}/testing', ['as' => 'testRoute', 'uses' => 'SomeController@someMethod']);
Route::get('test', function() {
echo route('testRoute', ['test' => 'green', 'a' => 'purple']);
});
This would output http://yourapp.local/some/green/purple/testing
这将输出 http://yourapp.local/some/green/purple/testing