이것이 내가 문제에 직면 한 경로입니다.
특정 도시의 등록 된 장소 목록을 가져 오는 경로
예 : 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] 삭제
몇 마디 만하겠습니다