在 MVC 中使用 $config 数组 类
Using $config array in MVC classes
我读了很多这方面的书,很多人说我应该使用单例 class。我正在考虑编写一个 "Config" class,它将在 __construct 上包含一个 "config.php" 文件,并循环遍历 $config 数组值并将它们放入 $this->values 中。 ..
但后来我读到越来越多关于如何永远不应该使用单例的内容。所以我的问题是 - 最好的方法是什么?我想要一个配置文件,用于组织目的,其中包含我所有的 $config 变量。我不想为了灵活性和诸如此类的目的而将诸如数据库用户名和密码之类的东西直接硬编码到方法中。
例如,我在我正在处理的 MVC 项目中使用了以下代码:
public/index.php
<?php
include '../app/bootstrap.php';
?>
app/bootstrap.php
<?php
session_start();
function __autoload ($class) {
$file = '../app/'.str_replace('_', '/', strtolower($class)).'.php';
if (file_exists($file)) {
include $file;
}
else {
die($file.' not found');
}
}
$router = new lib_router();
?>
app/lib/router.php
<?php
class lib_router {
private $controller = 'controller_home'; // default controller
private $method = 'index'; // default method
private $params = array();
public function __construct () {
$this->getUrl()->route();
}
private function getUrl () {
if (isset($_GET['url'])) {
$url = trim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
$this->controller = isset($url[0]) ? 'controller_'.ucwords($url[0]) : $this->controller;
$this->method = isset($url[1]) ? $url[1] : $this->method;
unset($url[0], $url[1]);
$this->params = array_values($url);
}
return $this;
}
public function route () {
if (class_exists($this->controller)) {
if (method_exists($this->controller, $this->method)) {
call_user_func(array(new $this->controller, $this->method), $this->params);
}
else {
die('Method '.$this->method.' does not exist');
}
}
else {
die('Class '.$this->controller.' does not exist');
}
}
}
?>
现在假设我访问了http://localhost/myproject/lead/test
里面会调用controller_leadclass和test方法
这是 app/controller/lead
的代码
<?php
class controller_lead extends controller_base {
public function test ($params) {
echo "lead test works!";
}
}
?>
app/controller/base
<?php
class controller_base {
private $db;
private $model;
public function __construct () {
$this->connect()->getModel();
}
private function connect () {
//$this->db = new PDO($config['db_type'] . ':host=' . $config['db_host'] . ';dbname=' . $config['db_name']. ', $config['db_user'], $config['db_pass'], $options);
return $this;
}
private function getModel () {
$model = str_replace('controller', 'model', get_class($this));
$this->model = new $model($this->db);
}
}
?>
这就是我 运行 进入问题的地方。如您所见,connect 方法将尝试创建一个新的 PDO 对象。考虑到我刚刚提供的所有其他代码,现在我要如何注入这个 $config 变量?
我的选择似乎是:
- 使用单例(不好)
- 使用全局(更差)
- 在 bootstrap.php 中包含 config.php,并在多个 class 中注入它(当 lib_router 与数据库完全无关?这听起来很糟糕。)
我还有什么其他选择?我不想做这三件事中的任何一件...
如有任何帮助,我们将不胜感激。
我最终在 bootstrap 中包含了一个配置文件,它只包含常量,并使用了这些常量。
I ended up including a config file in my bootstrap which simply contained constants, and used the constants.
我也是这样 - 需要 Model
中的文件。基于文件的方法是最合适的,因为它有一些好处:您可以.gitignore
、设置自定义权限集、集中存储所有参数等。
但是,如果配置文件仅包含数据库连接参数,我更愿意只在 Model
中要求配置文件。也许,您还可以分解为多个更具体的配置文件,并在必要时要求它们。
public function __construct()
{
if (self::$handle === FALSE)
{
$db = array();
require APP_DIR.DIR_SEP.'system'.DIR_SEP.'config'.DIR_SEP.'Database.php';
if (!empty($db))
{
$this->connect($db['db_host'], $db['db_user'], $db['db_password'], $db['db_name']);
}
else
{
Error::throw_error('Abimo Model : No database config found');
}
}
}
我读了很多这方面的书,很多人说我应该使用单例 class。我正在考虑编写一个 "Config" class,它将在 __construct 上包含一个 "config.php" 文件,并循环遍历 $config 数组值并将它们放入 $this->values 中。 ..
但后来我读到越来越多关于如何永远不应该使用单例的内容。所以我的问题是 - 最好的方法是什么?我想要一个配置文件,用于组织目的,其中包含我所有的 $config 变量。我不想为了灵活性和诸如此类的目的而将诸如数据库用户名和密码之类的东西直接硬编码到方法中。
例如,我在我正在处理的 MVC 项目中使用了以下代码:
public/index.php
<?php
include '../app/bootstrap.php';
?>
app/bootstrap.php
<?php
session_start();
function __autoload ($class) {
$file = '../app/'.str_replace('_', '/', strtolower($class)).'.php';
if (file_exists($file)) {
include $file;
}
else {
die($file.' not found');
}
}
$router = new lib_router();
?>
app/lib/router.php
<?php
class lib_router {
private $controller = 'controller_home'; // default controller
private $method = 'index'; // default method
private $params = array();
public function __construct () {
$this->getUrl()->route();
}
private function getUrl () {
if (isset($_GET['url'])) {
$url = trim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
$this->controller = isset($url[0]) ? 'controller_'.ucwords($url[0]) : $this->controller;
$this->method = isset($url[1]) ? $url[1] : $this->method;
unset($url[0], $url[1]);
$this->params = array_values($url);
}
return $this;
}
public function route () {
if (class_exists($this->controller)) {
if (method_exists($this->controller, $this->method)) {
call_user_func(array(new $this->controller, $this->method), $this->params);
}
else {
die('Method '.$this->method.' does not exist');
}
}
else {
die('Class '.$this->controller.' does not exist');
}
}
}
?>
现在假设我访问了http://localhost/myproject/lead/test
里面会调用controller_leadclass和test方法
这是 app/controller/lead
的代码<?php
class controller_lead extends controller_base {
public function test ($params) {
echo "lead test works!";
}
}
?>
app/controller/base
<?php
class controller_base {
private $db;
private $model;
public function __construct () {
$this->connect()->getModel();
}
private function connect () {
//$this->db = new PDO($config['db_type'] . ':host=' . $config['db_host'] . ';dbname=' . $config['db_name']. ', $config['db_user'], $config['db_pass'], $options);
return $this;
}
private function getModel () {
$model = str_replace('controller', 'model', get_class($this));
$this->model = new $model($this->db);
}
}
?>
这就是我 运行 进入问题的地方。如您所见,connect 方法将尝试创建一个新的 PDO 对象。考虑到我刚刚提供的所有其他代码,现在我要如何注入这个 $config 变量?
我的选择似乎是:
- 使用单例(不好)
- 使用全局(更差)
- 在 bootstrap.php 中包含 config.php,并在多个 class 中注入它(当 lib_router 与数据库完全无关?这听起来很糟糕。)
我还有什么其他选择?我不想做这三件事中的任何一件...
如有任何帮助,我们将不胜感激。
我最终在 bootstrap 中包含了一个配置文件,它只包含常量,并使用了这些常量。
I ended up including a config file in my bootstrap which simply contained constants, and used the constants.
我也是这样 - 需要 Model
中的文件。基于文件的方法是最合适的,因为它有一些好处:您可以.gitignore
、设置自定义权限集、集中存储所有参数等。
但是,如果配置文件仅包含数据库连接参数,我更愿意只在 Model
中要求配置文件。也许,您还可以分解为多个更具体的配置文件,并在必要时要求它们。
public function __construct()
{
if (self::$handle === FALSE)
{
$db = array();
require APP_DIR.DIR_SEP.'system'.DIR_SEP.'config'.DIR_SEP.'Database.php';
if (!empty($db))
{
$this->connect($db['db_host'], $db['db_user'], $db['db_password'], $db['db_name']);
}
else
{
Error::throw_error('Abimo Model : No database config found');
}
}
}