Search code examples
phplaravel-5laravel-5.1filemaker

Laravel single route point to different controller depending on slugs


I'm new to laravel and I have searched a lot for an answer to my problem but either it's not applicable or I'm not getting it.

I have a FileMaker solution for a client that handle customers and events. Each customer to my client have their own event websites that is managed via the solution. A cms simply. Each customer get a site with a url like clientsite.com/event.

Each page in the event has a page-type and I would like to address different controllers depending on the type.

In routes.php i have:

Route::group(['middleware' => ['sal', 'menu']], function () {

    Route::get('/{event}/{page}', function($event, $page) {
        // Query page for page-type and use controller depending on type
    });
});

There are many page types (standard text/image, specialized forms etc) and therefor I would like to address different controllers.

Event names are always unique but pages are not.


Solution

  • You could call a controller manually inside the route closure. Though I would suggest doing the validation in a helper file to make the route file clean and readable.

    Route::group(['middleware' => ['sal', 'menu']], function () {
    
        Route::get('/{event}/{page}', function($event, $page) {
            // you could do something like
            $user_type = Auth::user()->user_type;
            if($user_type == "organizer")
            {   
    
                $controller = $app->make('OrganizerController');  
                return $controller->callAction('controllerFunc', $parameters = array());          
            }
            else
            {
               $controller = $app->make('ClientController');  
               return $controller->callAction('controllerFunc', $parameters = array());          
            }        
    
        });
    });