Laravel 5 conditional routing and multiple controllers

So basically my app has two types of dynamic url..

  1. app.com/{page}
  2. app.com/{user}

Both having their own controllers

  1. PageController@index
  2. UserProfileController@index

But I’m struggling to get this working.

I have tried a few different methods. Here are two I have tried..

Route::get('{slug}', function($slug) {
    if (AppPage::where('slug', $slug)->count()) {
        // return redirect()->action('PageController@index', [$slug]);
        // return App::make('AppHttpControllersPageController', [$slug])->index();
        return 'Page found';
    } else if (AppUser::where('username', $slug)->count()) {
        // return redirect()->action('UserProfileController@index', [$slug]);
        // return App::make('AppHttpControllersUserProfileController', [$slug])->index();
        return 'User found';
    } else {
        return abort(404);
    }
});

I feel I should be doing this with middleware/filters. Any help would be great. Thanks.


Source: laravel

Leave a Reply