简体   繁体   中英

Laravel 5.5 passing controllers to routes groups

I don't know if this is possible. Let's say we have 3 route groups to create the prefixes...all coming off of root; so, something like the following could be problematic.

Route::group([
        'prefix' => '{something}',
        'middleware' => ['web', 'auth']
    ], function() {
});

So, there are n route groups. Each group has the same set of route patterns. The only difference is the controller that is used for each group.

<?php

Route::group([
        'prefix' => 'prefix1',
        'middleware' => ['web', 'auth']
    ], function() {
    $controller = '\Path\To\Controller';
    Route::get('/', $controller.'@index');
    Route::get('/{object}', $controller.'@handleObject');
});

Route::group([
        'prefix' => 'prefix2',
        'middleware' => ['web', 'auth']
    ], function() {
    $controller = '\Path\To\Controller2';
    Route::get('/', $controller.'@index');
    Route::get('/{object}', $controller.'@handleObject');
});

Route::group([
        'prefix' => 'prefix3',
        'middleware' => ['web', 'auth']
    ], function() {
    $controller = '\Path\To\Controller3';
    Route::get('/', $controller.'@index');
    Route::get('/{object}', $controller.'@handleObject');
});

What I'm looking for is something that would allow me to go from the route group to something else, passing the controller to it.

Is this possible? Is there an alternative that I'm missing?

Essentially the route groups consist of multiple sub-groups and routes and really, really do not want to have to essentially copy and paste the routes only to change the controllers.

Update

Almost looking to be able to something like this, which I and pretty sure cannot be done.

Route::group([
        'prefix' => ['prefix1', 'prefix2', 'prefix3'],
        'middleware' => ['web', 'auth']
    ], function() {
    $controller = null;
    if ($prefix == '...') {
        $controller = '\Path\To\Controller3';

    } else if ($prefix '...') {
        ...

    }
    Route::get('/', $controller.'@index');
    Route::get('/{object}', $controller.'@handleObject');
});

Yes, it's possible.

You just need an array and the logic. For the example below I use a config file within a service provider I developed. Then, just loop over the array and Laravel's router will do it every time.

// /config/service-config.php
$prefixes = [
    'tiles',
    'files',
    'kites',
    'foos'
];

// web.php
$controller = '\Path\To\Controller';

if (!is_null(config('service-config.prefixes'))) {
    foreach (config('service-config.prefixes') as $prefix) {    
        Route::group([
            'prefix' => $prefix
        ], function() use ($controller) {
            Route::post('/', $controller.'@addEmailAddress');
        });

        Route::group([
            'prefix' => $prefix .'/{foo}/bar'
        ], function() use ($controller) {
            // ...
        });
    }    
}

Results in:

/tiles
/tiles/{foo}/bar
/files
/files/{foo}/bar
/kites
/kites/{foo}/bar
/foos
/foos/{foo}/bar

Each uses the same controller and method in that controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM