Laravel single route point to different controller

2020-05-06 12:57发布

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.

2条回答
Bombasti
2楼-- · 2020-05-06 13:31

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());          
        }        

    });
});
查看更多
Juvenile、少年°
3楼-- · 2020-05-06 13:39

An alternative to the route solution could be to handle the logic in the controller itself:

First, update routes.php to something like:

Route::group(['middleware' => ['sal', 'menu']], function () {
    Route::get('/{event}/{page}', 'RoutesController@index');
});

Then, in the RoutesController.php file (add to app/Http/Controllers), you can do something similar to:

public function index()
{
    $event = Request::segment(1); // get the {event} part of the route
    $page = Request::segment(2); // get the {page} part of the route

    // get event data from database, e.g.
    $event_data = Event::where( 'slug', $event )->first();

    // load correct page
    switch ( $page ) {
        case "people":
            return $this->people();
        break;
        case "anotherPage":
            return $this->another_page();
        break;
    }
}

private function people()
{
    // show view
    return View::make('event.people');
}

This solution keeps your routes file clean, but also lets you handle the different event and page data, and load different views depending on the page being looked at. Your extra logic would be better in a controller rather than the routes file.

It all depends on where you prefer to code your page / view logic. You can use this approach call functions in the same controller, or external ones.

查看更多
登录 后发表回答