Laravel 5 在Laravel中的单个路由组中添加多个前缀

Laravel 5 在Laravel中的单个路由组中添加多个前缀,laravel-5,routes,Laravel 5,Routes,有没有办法在Laravel中的单个路由组中添加多个前缀 Route::group(['prefix' => 'prefix'], function (){ Route::get('hello', 'HelloController@sayHello'); }); 我尝试过使用管道添加- Route::group(['prefix' => 'prefix1|prefix2'], function (){ Route::get('hello', 'HelloControlle

有没有办法在Laravel中的单个路由组中添加多个前缀

Route::group(['prefix' => 'prefix'], function (){
   Route::get('hello', 'HelloController@sayHello');
});
我尝试过使用管道添加-

Route::group(['prefix' => 'prefix1|prefix2'], function (){
   Route::get('hello', 'HelloController@sayHello');
});
还尝试使用数组-

Route::group(['prefix' => ['prefix1', 'prefix2']], function (){
   Route::get('hello', 'HelloController@sayHello');
});

但是没有运气。真的有什么方法可以做到这一点吗?

您是否尝试过这样的
方法调用:

Route::group(['prefix' => '{prefix}'], function (){
    Route::get('hello', 'HelloController@sayHello')->where('prefix', 'prefix1|prefix2');
});
更新

如果您想以更有效的方式进行,您可以尝试一下,例如:

Route::group(['prefix' => '{prefix}'], function (){
    $routes = [];
    $routes[] = Route::get('hello', 'HelloController@sayHello');
    $routes[] = Route::get('other', 'HelloController@other');
    foreach($routes as $route) {
        $route->where('prefix', 'prefix1|prefix2');
    }
});

但这只是我的第一个想法。也许您可以找到另一个更好的方法。

您是否尝试过使用
where
方法调用,如下所示:

Route::group(['prefix' => '{prefix}'], function (){
    Route::get('hello', 'HelloController@sayHello')->where('prefix', 'prefix1|prefix2');
});
更新

如果您想以更有效的方式进行,您可以尝试一下,例如:

Route::group(['prefix' => '{prefix}'], function (){
    $routes = [];
    $routes[] = Route::get('hello', 'HelloController@sayHello');
    $routes[] = Route::get('other', 'HelloController@other');
    foreach($routes as $route) {
        $route->where('prefix', 'prefix1|prefix2');
    }
});

但这只是我的第一个想法。也许你能找到另一个更好的。

让我再加一个例子,只是为了处理偶尔的情况:

// The Route Name point to multiple prefixes 
// according to locale using the same Controller

$contactRoutes = function () {
    Route::get('', ['uses' => 'ContactController@index', 'as' => 'contact');
    Route::post('', ['uses' => 'ContactController@send', 'as' => 'contact.send');
};
switch (env('APP_LOCALE')) {
    case 'pt': 
        Route::group(['prefix' => 'contato'], $contactRoutes);
        break;
    case 'es':
        Route::group(['prefix' => 'contacto'], $contactRoutes);
        break;
    default: //en
        Route::group(['prefix' => 'contact'], $contactRoutes);
        break;
}

让我再添加一个示例,仅用于处理偶发情况:

// The Route Name point to multiple prefixes 
// according to locale using the same Controller

$contactRoutes = function () {
    Route::get('', ['uses' => 'ContactController@index', 'as' => 'contact');
    Route::post('', ['uses' => 'ContactController@send', 'as' => 'contact.send');
};
switch (env('APP_LOCALE')) {
    case 'pt': 
        Route::group(['prefix' => 'contato'], $contactRoutes);
        break;
    case 'es':
        Route::group(['prefix' => 'contacto'], $contactRoutes);
        break;
    default: //en
        Route::group(['prefix' => 'contact'], $contactRoutes);
        break;
}

它似乎起作用了。但是如果我需要在这个路由组中分组10-20条路由,那么在所有路由组中添加where子句将是低效的。那么,不可能用更简单的方法来做吗?也许你需要添加一些帮助函数或其他东西来让它更干净。我会在我的条目中给你推荐一个。非常感谢。:)仅供参考-如果使用此方法,请记住,此路由下的所有控制器操作都将接收前缀变量作为第一个参数,因为它将被视为路由参数。它似乎有效。但是如果我需要在这个路由组中分组10-20条路由,那么在所有路由组中添加where子句将是低效的。那么,不可能用更简单的方法来做吗?也许你需要添加一些帮助函数或其他东西来让它更干净。我会在我的条目中给你推荐一个。非常感谢。:)仅供参考-如果使用此方法,请记住此路由下的所有控制器操作都将接收前缀变量作为第一个参数,因为它将被视为路由参数。