简体   繁体   English

忽略Laravel中的可选参数?

[英]Ignore optional parameter in Laravel?

Currently within my routes file I've got the following Route defined: 当前,在我的路线文件中,我定义了以下路线:

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

And within the AdsController I've got the following function: 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";
}

The only issue is it appears that I must pass the $city variable? 唯一的问题是看来我必须传递$city变量? for instance, if I access http://example.com/mycountry/myregion/mycity/1 it works fine? 例如,如果我访问http://example.com/mycountry/myregion/mycity/1它可以正常工作吗? but if I was to run http://example.com/mycountry/myregion/1 it shows a 404 is their anyway around this other than creating multiple routes? 但是,如果我要运行http://example.com/mycountry/myregion/1则表明除创建多条路由外,他们是否仍在使用404

You would have to create two separate routes. 您将必须创建两个单独的路线。 Laravel won't interpret a blank slash (/) as a null variable. Laravel不会将斜杠(/)解释为空变量。

You could also pass a zero ('0') value in the route URI (example: /mycountry/myregion/0/1). 您还可以在路由URI中传递零('0')值(例如:/ mycountry / myregion / 0/1)。

Also, you could place the optional route parameters at the end of the route. 另外,您可以将可选的路线参数放在路线的末尾。 That may help. 这可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM