Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否忽略Laravel中的可选参数?_Php_Laravel - Fatal编程技术网

Php 是否忽略Laravel中的可选参数?

Php 是否忽略Laravel中的可选参数?,php,laravel,Php,Laravel,目前在我的路由文件中,我已定义了以下路由: Route::get('{country}/{county}/{city?}/{id}', 'AdsController@show')->name('show')->where('id', '[0-9]+'); 在AdsController中,我有以下功能: public function show($country, $region, $city = null, $id) { if($city !== null)

目前在我的路由文件中,我已定义了以下路由:

Route::get('{country}/{county}/{city?}/{id}', 'AdsController@show')->name('show')->where('id', '[0-9]+');
AdsController
中,我有以下功能:

public function show($country, $region, $city = null, $id) {
    if($city !== null)
        echo "we do something here extra";
    echo "Let's run our normal function";
}

唯一的问题是我似乎必须传递
$city
变量?例如,如果我访问
http://example.com/mycountry/myregion/mycity/1
它工作正常吗?但是如果我要运行
http://example.com/mycountry/myregion/1
它显示了一个404除了创建多个路由之外,他们是否还可以创建其他路由?

您必须创建两个单独的路由。Laravel不会将空白斜杠(/)解释为空变量

您还可以在路由URI中传递零('0')值(例如:/mycountry/myregion/0/1)


此外,您还可以将可选管线参数放置在管线末端。这可能会有所帮助。

可选的东西不要放在必需的东西之前,在方法签名中也一样,这是没有意义的