这些是我面临问题的路线
路线以获取特定城市中已注册地点的列表
例如: http:// localhost:8000 / London,http:// localhost:8000 / London / Restaurants
Route::group(['namespace' => 'Page'], function() {
Route::group(['prefix' => '{city}', 'where' => ['city' => '[\w\d]+']], function() {
Route::get('/', 'CityPageController@showCityPage')->name('cityPage');
});
});
路由获取特定的用户个人资料及其详细信息,例如评论,照片等。
例如: http:// localhost:8000 / John,http:// localhost:8000 / John / reviews,http:// localhost:8000 / John / photos
Route::group(['namespace' => 'User'], function() {
Route::group(['middleware' => 'verified'], function() {
Route::group(['prefix' => '{username}', 'where' => ['username' => '[\w\d]+']], function() {
Route::get('/', 'ProfileController@showProfilePage')->name('profilePage');
Route::get('/reviews', 'ReviewController@showReviewPage')->name('reviewPage');
Route::get('/photos', 'ImageController@showPhotoPage')->name('photoPage');
});
});
});
问题是这两个路由不能同时工作。
居于另一个之上的路由优先于另一个路由。
如何解决这个路由问题。
编辑
我知道有一种方法可以实现此功能,但我不知道如何。任何帮助表示赞赏。
您的路线文件
Route::get('{slug1}', 'PageController@singleSlug'); # slug 1 has to be unique i.e. username and cityname
Route::get('{slug1}/{slug2}', 'PageController@doubleSlug'); # combination of slug1 and slug2 has to be unique
控制器功能
public function singleSlug($slug1)
{
$user = User::where('name', $slug1)->first();
if ($user) {
return view('user')->compact('user');
}
$city = City::where('name', $slug1)->first();
if ($city) {
return view('city')->compact('city');
}
abort(404); # neither user nor city
}
public function doubleSlug($slug1, $slug2)
{
// check the slug2 as this value is always defined by the system
switch ($slug2) {
case 'Restaurants':
$city = City::with('restaurants')->where('name', $slug1)->first();
if ($city) {
$viewName = 'city_restos_listing';
$viewData = $city;
}
break;
case 'reviews':
$user = User::with('reviews')->where('name', $slug1)->first();
if ($user) {
$viewName = 'user_reviews_listing';
$viewData = $user;
}
break;
case 'photos':
$user = User::with('photos')->where('name', $slug1)->first();
if ($user) {
$viewName = 'user_photos_listing';
$viewData = $user;
}
break;
default:
abort(404); # the slug 2 is incorrect
break;
}
if(isset($viewName)) {
return view($viewName)->compact('viewData');
} else {
abort(404); # user or city not found
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句