我会这样做:
将允许的国家列表放入配置文件 (countries.php)。
在routes.php:
// choosing country
$country = '';
if (in_array(Request::segment(1), Config::get('countries'))) {
$country = Request::segment(1);
}
// making route for top level
if ($country != '') {
Route::any( '/', 'MainPage@index');
}
// making routes optionally prefixed by country
Route::group(
array('prefix' => $country,
function () {
// here all routes
});
在您定义连接的database.php 中,您可以添加另一个连接,例如:
'germany' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'germany_connection',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
现在在同一个文件中(尽管您可能应该将它移到其他地方)您可以这样做:
if ($country == 'germany') {
DB::disconnect();
Config::set('database.default','germany');
DB::reconnect();
}
您当然可以在这里添加许多条件,或者如果您为每个允许的国家/地区定义了连接,您可以简单地这样做:
if ($country != '' ) {
DB::disconnect();
Config::set('database.default', $country);
DB::reconnect();
}
应该可以,但是我没有测试过