How about this?
geocine! Friend, I got to see this post. But not served to me because al the definition of bank is being done via code. I need something automatic. In a way that moving the client, which can be a parameter, it already search the database data connection such as a client and Config :: set the secondary connection. Something like that!
You can create dynamic DB connections in laravel with following syntax
Config::set('database.connections.key', array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'dbuser',
'password' => 'dbpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
With that DB::connection('key'); would just work.
@source: http://stackoverflow.com/questions/14524181/laravel-4-multiple-tenant-application-each-tenant-its-own-database-and-one-gl
Does not have a way to do this in the before filter?
Anyone know where's the error I created this filter to setting a new connection, but not okay by setting ..
filter.php
Route::filter('defineConnection', function($request) {
if (Session::has('slug_cliente')) {
$cliente = Session::get('slug_cliente');
$minutes = 30;
$cli = Cache::remember('connection' . $cliente, $minutes, function() use ($cliente) {
return DB::connection("admin")
->table("clientes")
->where("code", "=", $cliente)
->first();
});
if (count($_cli)) {
//Esse cara aqui eu criaria um "Filter" para usar com before -> "defineConnection" Fazendo essa verifica
Config::set("database.conections.cliente.driver", "mysql");
Config::set("database.conections.cliente.host", $_cli->host);
Config::set("database.conections.cliente.database", $_cli->database);
Config::set("database.conections.cliente.user", $_cli->username);
Config::set("database.conections.cliente.password", $_cli->password);
Config::set("database.default", "cliente");
} else {
return Redirect::to("/");
}
} else {
return Redirect::to("/");
}
});
=================================================
route.php
Route::get('login/{slug_cliente?}', function($slug_cliente = NULL) {
if (is_null($slug_cliente)) {
return Redirect::to("/");
}
//Se for Nulo, redireciona para "/" sem logar.
Session::put('slug_cliente', $slug_cliente);
return View::make("login");
});
Route::post('login/{slug_cliente}', array('before' => 'defineConnection', function($slug_cliente = null) {
$credentials = array(
'email' => Input::get("email"),
'password' => Input::get("password"),
);
$user = Sentry::authenticate($credentials, false);
if (!$user) {
return Redirect::to("dashboard");
} else {
return Redirect::back();
}
}));
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community