Laravel 4 可选路由参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18846688/
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 08:26:27  来源:igfitidea点击:

Laravel 4 Optional Route Parameter

routinglaravel

提问by davidcoder

I would like to know how to add an optional route parameter for a controller method:

我想知道如何为控制器方法添加可选的路由参数:

Currently I have a route, shown below:

目前我有一条路线,如下所示:

Route::get('devices/{code}/{area}','HomeController@getDevices');

and a controller method:

和控制器方法:

public function getDevices($code=NULL,$area) {...}

My get request will look like:

我的获取请求将如下所示:

/devices/A/ABC

It's working fine, but I want the {code} parameter to be optional so that I can get data in different ways:

它工作正常,但我希望 {code} 参数是可选的,以便我可以通过不同的方式获取数据:

/devices//ABC or 
/devices/ABC

I've tried the following, but all failed with NotFoundHttpException

我尝试了以下操作,但都失败了 NotFoundHttpException

Route::get('devices/{code?}/{area}','HomeController@getDevices'); Route::get('devices/(:any?)/{area}','HomeController@getDevices');

Route::get('devices/{code?}/{area}','HomeController@getDevices'); Route::get('devices/(:any?)/{area}','HomeController@getDevices');

Thanks for your help.

谢谢你的帮助。

回答by Sergiu Paraschiv

The optional parameter needs to be at the end of the URL.

可选参数需要位于 URL 的末尾。

So yours is a clear Incorrect usage of default function arguments, as described here. This is the reason your code does not work as you expect it to.

所以你是一个明确的函数默认参数不正确的用法,如所描述这里。这就是您的代码无法按预期工作的原因。

You'll have to reverse the order of those two parameters or implement different methods for those cases, taking into account that you'll need some sort of prefix to differentiate between them:

您必须颠倒这两个参数的顺序或为这些情况实现不同的方法,考虑到您需要某种前缀来区分它们:

Route::get('devices/area/{area}','HomeController@getDevicesByArea');
Route::get('devices/code-and-area/{code}/{area}','HomeController@getDevicesByAreaAndCode');

public function getDevicesByAreaAndCode($area, $code = NULL) {...}
public function getDevicesByArea($area) { 
    return $this->getDevicesByAreaAndCode($area);
}

OR, as I said before, reverse the parameters:

或者,正如我之前所说,反转参数:

Route::get('devices/area-and-code/{area}/{code?}','HomeController@getDevicesByAreaAndCode');

public function getDevicesByAreaAndCode($area, $code = NULL) {...}

回答by Jason

You can do this with Laravel 4 if you want, and it may be convenient for some JSON calls where a parameter not on the end of the URI may need to be empty.

如果需要,您可以使用 Laravel 4 执行此操作,并且对于某些 JSON 调用可能很方便,其中 URI 末尾的参数可能需要为空。

The key is setting up a route specifically for the empty parameter. This route:

关键是专门为空参数设置路由。这条路线:

Route::get('devices//{area}','HomeController@getDevicesByArea');

will catch the URI "devices//myarea" and send it to:

将捕获 URI "devices//myarea" 并将其发送到:

public function getDevicesByArea($area) {...}

Where the code is supplied, the main route can catch that:

在提供代码的地方,主路由可以捕获:

Route::get('devices/{code}/{area?}','HomeController@getDevicesByCode');

sending the code and optional area to:

将代码和可选区域发送至:

public function getDevicesByArea($code, $area = '') {...}

This is not to say that swapping the parameters around in this example is not the better solution especially if the URL is going to be handled by a human. But I'm just adding for the record here that what was originally requested is possible, and can make some AJAX requests easier to deal with.

这并不是说在这个例子中交换参数不是更好的解决方案,尤其是当 URL 将由人工处理时。但我只是在这里添加记录,最初请求的内容是可能的,并且可以使一些 AJAX 请求更容易处理。