Laravel 4:如何传递多个可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23627864/
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
Laravel 4 : How to pass multiple optional parameters
提问by user3631427
I am new to laravel and I am really struggling to understand how to pass multiple optional url parameters.
我是 laravel 的新手,我真的很难理解如何传递多个可选的 url 参数。
What is the standard way to code routes when passing 3 optional parameters to the controller?
将 3 个可选参数传递给控制器时,编码路由的标准方法是什么?
Also is there a way to code a route to allow named parameters to be passed to the controller?
还有一种方法可以编写路由以允许将命名参数传递给控制器吗?
such as
如
public/test/id=1&page=2&opt=1
or
public/test/id=1/page=2/opt=1
Thanks for any help
谢谢你的帮助
回答by Razor
If you have multiple optional parameters
如果您有多个可选参数
Route::get('test',array('as'=>'test','uses'=>'HomeController@index'));
And inside your Controller
在你的控制器里面
class HomeController extends BaseController {
public function index()
{
// for example public/test/id=1&page=2&opt=1
if(Input::has('id'))
echo Input::get('id'); // print 1
if(Input::has('page'))
echo Input::get('page'); // print 2
//...
}
}
回答by alexrussell
Named parameters are usually done as route segments but without explicit naming. So for example you could o something like this:
命名参数通常作为路由段完成,但没有明确命名。因此,例如,您可以这样做:
Route:get('test/{id?}/{page?}/{opt?}', function ($id = null, $page = null, $opt = null) {
// do something
});
$id
, $page
and $opt
are all optional here as defined by the ?
in the segment definitions, and the fact that they have default values in the function. However, you'll notice there's something of a problem here:
$id
,$page
和$opt
在这里都是可选的?
,由段定义中的 定义,以及它们在函数中具有默认值的事实。但是,您会注意到这里存在一些问题:
- They have to appear in the URL in the correct order
- Only
$opt
is truly optional,$page
must be supplied if$opt
is, and$id
must be if$page
is
- 它们必须以正确的顺序出现在 URL 中
- 只有
$opt
在真正可选的,$page
必须提供,如果$opt
是,而且$id
必须是如果$page
IS
This is a limitation brought about by the way that Laravel maps the named segments to function/method parameters. You could theoretically implement your own logic to make this work, however:
这是 Laravel 将命名段映射到函数/方法参数的方式带来的限制。理论上,您可以实现自己的逻辑来完成这项工作,但是:
Route:get('test/{first?}/{second?}/{third?}', function ($first = null, $second = null, $third = null) {
if ($first) {
list($name, $value) = @explode('=', $first, 2);
$$name = $value;
}
if ($second) {
list($name, $value) = @explode('=', $second, 2);
$$name = $value;
}
if ($third) {
list($name, $value) = @explode('=', $third, 2);
$$name = $value;
}
// you should now have $id, $page and $opt defined if they were specified in the segments
});
Not that this is a very na?ve solution, relying on blind exploding by =
as well as setting the name of an arbitrarily-inputted variable (which is obviously asking for trouble). You should add more checking to this code, but it should give you an idea of how to get over the aforementioned two problems.
并不是说这是一个非常幼稚的解决方案,依靠盲目爆炸=
以及设置任意输入变量的名称(这显然是在自找麻烦)。您应该向此代码添加更多检查,但它应该让您了解如何解决上述两个问题。
It should probably be noted that this is kinda going against the 'right way' to do routing and URIs in Laravel, so unless you reallyneed this functionality, you should rethink the way you are forming these URIs to a way that the Laravel framework is more set-up for.
可能应该注意的是,这有点违背在 Laravel 中进行路由和 URI 的“正确方法”,因此除非您真的需要此功能,否则您应该重新考虑形成这些 URI 的方式,使其符合 Laravel 框架的要求更多设置。