Search code examples
laravelrouteslaravel-routinglaravel-route

how to pass different dynamic parameters at same position to access different view pages in laravel


These are my routes for which I am facing problem

Route to get the list of registered places in a particular city

Ex: 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');
    });
});

Route to get a particular user profile and its details such as reviews, photos etc.

Ex: 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');
        });
    });
});

The problem is that both of these routes are not working simultaneously.

The route the resides above the other takes precedence over the other route.

how to solve this problem of routing.

Edit

I know there is a way to achieve this functionality but I don't know how. Any help is appreciated.


Solution

  • Note: If you haven't, firstly I recommend to create a unique slug field on database first that will appear on url

    Your route file

    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
    

    The controller functions

    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
     }
    }