您可能对使用Laravel 文档中所述的可选路径参数感兴趣。这意味着您将拥有:
Route::get('gegonota/{gid}/{cid}/{nid?}', [
'uses' => 'GegonosController@index',
'as' => 'gegonota'
]);
希望这能解决问题。
更新
即使我不能说这是解决方法,因为您说重新排列变量并没有解决问题。我宁愿将这些可选变量作为请求参数传递以使事情变得简单,即我的 url 看起来像:
http://localhost:8000/gegonota/?gid=&cid=&nid=
///因此,我已经可以将每个预期参数的默认值设置为 null,而不是处理我的 url中可能出现的这种奇怪的不一致:
//In a controller
public funtion index()
{
//put them in an array
$my_variables = request()->only(['gid', 'cid', 'nid']);
//or this way
$gid = request()->get('gid');
$cid = request()->get('cid');
$nid = request()->get('nid');
//they are default to null if not existing or have no value
}
这意味着您的路线声明很简单,即:
Route::get('gegonota', [
'uses' => 'GegonosController@index',
'as' => 'gegonota'
])
除非有特殊需要将这些可选变量传递给路径,否则将其作为请求参数显然更容易更好。希望这会更好。