我的路线中有此代码
Route::controller('/orders/{from}/{to}', 'CartController');
链接是这样的
localhost/admin/orders/2020-01-01/2020-01-02
这将打开两个日期之间的所有记录
但是我还有另一个类似这样的链接
localhost/admin/orders/4212
在新选项卡上打开特定行
此2个链接属于1个功能
称为
public function getIndex($from,$to){
}
我可以将此参数设为可选参数吗?我的route.php中有1 Route::controller('/orders/{from}/{to}', 'CartController');
?
您可以将任何参数设置为可选,只需在参数名称的末尾附加?
。简单示例:
Route::controller('/orders/{from?}/{to?}', 'CartController');
此外,我还注意到您没有在路由定义中调用任何控制器动作。如果您希望此路由导致您的getIndex()方法,请将其更改为:
Route::controller('/orders/{from?}/{to?}', 'CartController@getIndex');
阅读有关官方documentation的更多信息。
尝试一下:
Route::controller('/orders/{from?}/{to?}', 'CartController');
public function getIndex($from = false,$to = false){
}
您可以这样使用
Route::controller('/orders/{from}/{to?}', 'CartController');
并且在您的控制器中
public function getIndex($from,$to=null){
if($to==null){
//to open specific row on a new tab
}
else{
// Other task
}
}
在这种情况下,如果您愿意这样做,最好考虑使用POST
inteadGET
intead作为下面的答案,并考虑要仅将$to
放入网址中的senerio,然后将其用作[ C0]以获得$from
,您必须先发送$to
$from
路线:
public function getIndex($from = null, $to = null , $unknown = null){
}