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

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

Laravel 4 : How to pass multiple optional parameters

phplaravellaravel-routing

提问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, $pageand $optare 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在这里都是可选的?,由段定义中的 定义,以及它们在函数中具有默认值的事实。但是,您会注意到这里存在一些问题:

  1. They have to appear in the URL in the correct order
  2. Only $optis truly optional, $pagemust be supplied if $optis, and $idmust be if $pageis
  1. 它们必须以正确的顺序出现在 URL 中
  2. 只有$opt在真正可选的,$page必须提供,如果$opt是,而且$id必须是如果$pageIS

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 框架的要求更多设置。