laravel 9路由模型绑定多个可选参数



我已经定义了这个路由:

Route::get('/categories/{category}/{country}/{state?}/{county?}/{city?}', ['AppHttpControllersLocationController', 'show'])->withScopedBindings();
public function show(Category $category, Country $country, State $state = null, County $county = null, City $city = null) {
echo 'ok';
}

这很好,自动检查关系,工作与1,2或3个可选参数。但是…我想扩展它,这样郡就不总是强制性的。因为有些城市与state有直接关系,中间没有county_id。Cities表有county_id和state_id,并且总是只有其中一个存在。如果我加上:

Route::get('/categories/{category}/{country}/{state?}/{city?}', ['AppHttpControllersLocationController', 'show'])->withScopedBindings();

只有一条路由是正常的。

我该如何解决这个问题?谢谢。

你可以定义两个不同的路由

Route::get(
'/categories/{category}/{country}/{state?}/{county?}', 
['AppHttpControllersLocationController', 'show']
)
->withScopedBindings()
->name('categories.show.county');
Route::get(
'/categories/{category}/{country}/{state?}/{county?}/{city?}', 
['AppHttpControllersLocationController', 'show']
)
->withScopedBindings()
->name('categories.show.county.city');

然后检查控制器中的路由名


public function show(
Category $category, 
Country $country, 
State $state = null
) {
if(
request()->route()->named('categories.show.county') &&
request()->route()->hasParameter('county')
) {
$county = County::where(
(new County)->getRouteKeyName(),request()->route('county')
)
->firstOrFail();
}
if(request()->route()->named('categories.show.county.city')) {
if(request()->route()->hasParameter('county') {
$county = County::where(
(new County)->getRouteKeyName(),request()->route('county')
)
->firstOrFail();
}

if(request()->route()->hasParameter('city')) {
$city = City::where(
(new City)->getRouteKeyName(),request()->route('city')
)
->firstOrFail();
}
}
}

所以,根据Donkarnash的回答,我终于有了一个解决方案。

Route::get('/categories/{category}/{country}/{state?}/{county_slug?}/{city_slug?}',
['AppHttpControllersLocationController', 'show'])->scopeBindings();
public function show(
Category $category,
Country $country,
State $state = null,
$county_slug = null,
$city_slug = null
) {
if ($county_slug && $city_slug)
{
// two parameters present, that means the chain is state -> county -> city
$county = County::where('state_id', $state->id)
->where('slug', $county_slug)
->firstOrFail();
$city = City::where('county_id', $county->id)
->where('slug', $city_slug)
->firstOrFail();
} else {
if ($county_slug) {
// one parameter present, that means the chain is state -> county OR state -> city
$county = County::where('state_id', $state->id)
->where('slug', $county_slug)
->first();
if (!$county) {
$city_slug = $county_slug;
$city = City::where('state_id', $state->id)
->where('slug', $city_slug)
->first();
}
if (!$county && !$city) {
abort(404);
}
}
}
}

然后在migrations状态表中:

$table->unique(['slug', 'country_id']);

县表:

$table->unique(['slug', 'state_id']);

城市表:

$table->unique(['slug', 'state_id']);
$table->unique(['slug', 'county_id']);

它起作用了。唯一的缺点是,如果一个县和一个城市有相同的蛞蝓属于同一个州。例如,县与段塞语&;test&;state_id "15"城市与鼻涕虫"测试";state_id"15"。那么它就不能正常工作,结果是县。但通常一个国家的所有城市都有一个国家链。状态→县→城市还是乡村->状态→所以这个小缺点不会影响网站的最终结果。然而,这也可以通过调整请求验证规则来解决。