[英]For PHP REST API, how can I define/set the database connection parameters outside the class definition?
I'm using the PHP REST API example that appears all over the web. 我正在使用遍布Web的PHP REST API示例。 Here's basically what it is: 基本上它是这样的:
<?php
require_once("Rest.inc.php");
class API extends REST {
const DB_SERVER = "myhost";
const DB_USER = "myuser";
const DB_PASSWORD = "mypassword";
const DB_NAME = "mydb";
public $data = "";
private $db = NULL;
public function __construct(){
parent::__construct();
$this->dbConnect();
}
private function dbConnect(){
try {
$this->db = new PDO("mysql:host=" . self::DB_SERVER . ";dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
public function processApi(){
$func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
if((int)method_exists($this,$func) > 0) {
$this->$func();
} else {
$this->response('',404);
}
}
private function activity(){
...
}
private function client(){
...
}
private function facility(){
...
}
etc...
private function json($data){
if(is_array($data)){
return json_encode($data);
}
}
}
// Initiiate Library
$api = new API;
$api->processApi();
?>
It works fine, except that I don't want to hardcode the database connection parameters in that script. 它工作正常,但我不想在该脚本中硬编码数据库连接参数。 What is the best way (or is there a way) to separate them from this script? 将它们与此脚本分开的最佳方法是什么(或者有办法)是什么? I can't use an include file inside the class declaration. 我不能在类声明中使用include文件。 Can I put the whole dbConnect() function in an include file, and include that outside the class definition? 我可以将整个dbConnect()函数放在包含文件中,并将其包含在类定义之外吗? Any other options? 还有其他选择吗? I just want it so that there is nothing tying this script to a specific server. 我只是想要它,以便没有任何东西将这个脚本绑定到特定的服务器。 I would prefer to have the db server defined in a separate file that could be different on each server. 我希望将db服务器定义在一个单独的文件中,每个服务器上的文件可能不同。 I'm new to PHP and just wondering how the experts handle this. 我是PHP的新手,只是想知道专家如何处理这个问题。
**Edit: Got it working! **编辑:搞定了! This is a bit of a hybrid of the supplied answers. 这是所提供答案的混合体。
dbConnect.php dbConnect.php
<?php
class DB {
const DB_SERVER = "myserver";
const DB_USER = "myuser";
const DB_PASSWORD = "mypassword";
const DB_NAME = "mydb";
private $db = null;
public function __construct() {
try {
$this->db = new PDO("mysql:host=" . self::DB_SERVER . ";dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
?>
api.php api.php
<?php
require_once("Rest.inc.php");
require_once("dbConnect.php");
class API extends REST {
public $data = "";
private $db = null;
public function __construct(DB $mydb){
parent::__construct();
$this->db = mydb->db;
}
}
// Initiiate Library
$api = new API(new DB());
$api->processApi();
?>
You can use like below: 你可以使用如下:
db.php: db.php中:
create connection function in separate class. 在单独的类中创建连接函数。
class DB {
const DB_SERVER = "myhost";
const DB_USER = "myuser";
const DB_PASSWORD = "mypassword";
const DB_NAME = "mydb";
public $data = "";
private $db = NULL;
private function dbConnect(){
try {
$this->db = new PDO("mysql:host=" . self::DB_SERVER . ";dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
current_file.php : current_file.php :
class API extends REST {
public function __construct(){
parent::__construct();
//$this->dbConnect();
//here you can implement the class and function
$this->db = new DB();
$this->db->dbConnect();
}
}
You need to use Dependency Injection
pattern, it will help you. 您需要使用Dependency Injection
模式,它会对您有所帮助。 And you will have something like this: 你会得到这样的东西:
interface DB
{
// Here you need to declare your common methods for all DB classes.
}
class MasterDB implements DB
{
const DB_SERVER = "myhost";
const DB_USER = "myuser";
const DB_PASSWORD = "mypassword";
const DB_NAME = "mydb";
private $db = null;
private function __construct()
{
try {
$this->db = new PDO("mysql:host=" . self::DB_SERVER . ";dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
class SlaveDB implements DB
{
// Here you need to provide another connection
}
class API extends REST
{
private $db = null;
public function __construct(DB $db)
{
parent::__construct();
$this->db = $db;
}
}
$api = new API(new MasterDB());
// or
$api = new API(new SlaveDB());
$api->processApi();
It is not a perfect example, but this one will help you solve your problem! 这不是一个完美的例子,但这一个将帮助您解决您的问题! Next step probably is - create parent or abstract class for MasterDB
and SlaveDB
... 下一步可能是 - 为MasterDB
和SlaveDB
创建父类或抽象类...
I would suggest you to define a ini configuration file, and configure the database configuration inside the ini file. 我建议你定义一个ini配置文件,并在ini文件中配置数据库配置。
Use the method parse_ini_file
to get the configurations from ini file. 使用方法parse_ini_file
从ini文件中获取配置。 By this way, you will have different configurations for each server. 通过这种方式,您将为每个服务器配置不同的配置。
Documentation on creating and parsing ini file can be found here 有关创建和解析ini文件的文档,请访问此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.