我使用Slim Framework在PHP中构建了一个简单的RESTful API。 我还是OOP的新手。我有一个使用PDO查询数据库的路由。 现在我想用更多路由扩展API,但我需要连接到不同的数据库。
示例路线:
$app->get('/usados/carrocarias', 'authenticate', function() {
$response = array();
$db = new DbHandler();
// fetching all carrocarias
$carrocarias = $db->getCarrocariasUsados();
$response["error"] = false;
$response["carrocarias"] = array();
array_walk_recursive($carrocarias, function(&$val) {
$val = utf8_encode((string)$val);
});
array_push($response["carrocarias"], $carrocarias);
echoRespnse(200, $response, "carrocariasoutput");
})->via('GET', 'POST');
我的DbHandler课程:
class DbHandler {
private $conn;
function __construct() {
require_once dirname(__FILE__) . '/DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
...
}
我的DbConnect文件:
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . '/Config.php';
// Connecting to mysql database
$this->conn = new PDO("dblib:host=".DB_HOST.";dbname=".DB_NAME.";charset=UTF-8", DB_USERNAME, DB_PASSWORD);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// returing connection resource
return $this->conn;
}
}
DB_HOST,DB_NAME,DB_USERNAME和DB_PASSWORD在Config.php中定义。我知道我需要为其他数据库添加其他凭据到Config.php。
如何修改我的类和路由,以便我可以传递一个变量来决定连接哪个数据库?
编辑:我正在用我认为的解决方案来完成我的问题。至少对我来说,我希望它可以帮助我做其他新手。不知道这是不是最好的做法。
我的DBConnect.php
<?php
class DbConnect {
private $conn;
public $selDatabase;
function __construct($strServer) {
$this->selDatabase = $strServer;
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . '/Config.php';
try {
switch($this->selDatabase) {
case 'mysql-localhost':
$this->conn = new PDO('mysql:host=localhost;dbname=task_manager','root', 'mkecdapl');
break;
case 'sqlsrv-localhost':
$this->conn = new PDO("sqlsrv:server=".DB_HOST.";Database=".DB_NAME_NOVOS.";charset=UTF-8", DB_USERNAME, DB_PASSWORD) or die("failed to connect");
break;
case 'dbusados-localhost':
$this->conn = new PDO("sqlsrv:server=".DB_HOST.";Database=".DB_NAME_USADOS, DB_USERNAME, DB_PASSWORD) or die("failed to connect");
break;
case 'dbusados':
$this->conn = new PDO("dblib:host=".DB_HOST.";dbname=".DB_NAME_USADOS, DB_USERNAME, DB_PASSWORD) or die('failed to connect');
break;
case 'dbnovos-localhost':
$this->conn = new PDO("sqlsrv:server=".DB_HOST.";Database=".DB_NAME_NOVOS, DB_USERNAME, DB_PASSWORD) or die("failed to connect");
break;
case 'dbnovos':
$this->conn = new PDO("dblib:host=".DB_HOST.";dbname=".DB_NAME_NOVOS, DB_USERNAME, DB_PASSWORD) or die('failed to connect');
break;
case 'dbnovas':
$this->conn = new PDO("dblib:host=".DB_HOST.";dbname=".DB_NAME_NOVAS, DB_USERNAME, DB_PASSWORD) or die('failed to connect');
break;
}
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// returing connection resource
return $this->conn;
} catch (PDOException $e) {
echo "DataBase Error: ".$e->getCode()." ".$e->getMessage();
}
}
}
&GT;
我的DBHandler是一样的。 区别在于我的路线。而不是:
$db = new DbHandler();
我使用相应的数据库名称调用相同的方法:
$db = new DbHandler('dbusados');
答案 0 :(得分:2)
将每个数据库连接添加为$ app对象的依赖项。
您可以设置不同的静态方法来返回正确的PDO对象。
$app->db1 = DbConnect::getDatabase1;
class DbHandler {
private $conn;
public static function getDatabase1() {
require_once dirname(__FILE__) . '/DbConnect_Database1.php';
// opening db connection
$db = new DbConnect();
$db->conn = $db->connect();
return $db;
}
...
}