我在PHP中创建一个基本的MVC结构化CMS,作为学习MVC如何工作的一种方法(因此我没有使用真正的预构建引擎)。我有一个基本版本工作,我正在尝试将信息转移到配置文件,如:
的config.php
<?php
return array(
// MySQL database details
'database' => array(
'host' => 'localhost',
'port' => '',
'username' => '',
'password' => '',
'name' => '',
'collation' => 'utf8_bin'
),
// Application settings
'application' => array(
// url paths
'default_controller' => 'index',
'default_action' => 'index',
'timezone' => 'UTC',
),
// Session details
'session' => array(
'name' => '',
'expire' => 3600,
'path' => '/',
'domain' => ''
),
// Error handling
'error' => array(
'ignore' => array(E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED),
'detail' => false,
'log' => false
)
);
我将它包含在index.php文件中,如下所示:
的index.php
define ('__SITE_PATH', $site_path);
$config = include __SITE_PATH . '/config.php';
但是,当我尝试稍后在模板文件中加载它以进行测试时,或者任何其他文件都没有返回任何内容。这是课程的问题吗?如果有人能够对此事有所了解,我会非常感激。
以下是完整的index.php以供参考:
<?php
/*** error reporting on ***/
error_reporting(E_ALL);
/*** define the site path ***/
$site_path = realpath(dirname(__FILE__));
define ('__SITE_PATH', $site_path);
$config = include __SITE_PATH . '/config.php';
/*** include the controller class ***/
include __SITE_PATH . '/application/' . 'controller_base.class.php';
/*** include the registry class ***/
include __SITE_PATH . '/application/' . 'registry.class.php';
/*** include the router class ***/
include __SITE_PATH . '/application/' . 'router.class.php';
/*** include the template class ***/
include __SITE_PATH . '/application/' . 'template.class.php';
/*** auto load model classes ***/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = __SITE_PATH . '/model/' . $filename;
if (file_exists($file) == false) {
return false;
}
include ($file);
}
/*** a new registry object ***/
$registry = new registry;
/*** load the router ***/
$registry->router = new router($registry);
/*** set the controller path ***/
$registry->router->setPath (__SITE_PATH . '/controller');
/*** load up the template ***/
$registry->template = new template($registry);
/*** load the controller ***/
$registry->router->loader();
答案 0 :(得分:4)
我认为你这样做是错的。
而不是创建复杂的配置,我发现在引导阶段初始化所有结构要容易得多,然后在需要时inject the dependencies。
例如,为了拥有数据库的连接配置,您需要创建一个闭包,然后可以在data mappers的工厂中注入。
$dbhProvider = function()
{
$instance = new \PDO('mysql:host=localhost;dbname=foobar;charset=UTF-8',
'user',
'password');
$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $instance;
};
类似的原则可用于处理会话管理和路由机制。
这到底是什么?
'ignore'=&gt;数组(E_NOTICE,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED),
为什么要忽略与代码质量相关的警告?那只是愚蠢的。