这是我的应用程序的结构:
core
application
controller_base.class.php
registry.class.php
router.class.php
template.class.php
controller
include
config.php
model
views
index.php
文件config.php包含在index.php文件中(这没有问题),以下是它包含的内容:
<?php
/*** include the controller class ***/
include(ROOT . '/core/application/controller_base.class.php');
/*** include the registry class ***/
include(ROOT . '/core/application/registry.class.php');
/*** include the router class ***/
include(ROOT . '/core/application/router.class.php');
/*** include the template class ***/
include(ROOT . '/core/application/template.class.php');
/*** autoload model classes ***/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = ROOT . '/core/model/' . $filename;
if (!file_exists($file)) {
return false;
}
// include the $class_name class
include($file);
}
$registry = new registry;
你必须知道常量 ROOT 是这样定义的:
define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
此常量包含值/Lab/Livre/POO/TP1
,这当然是应用程序的根。
但是我在config.php中包含的所有文件都存在问题。PHP 给我抛出一个错误,说在/Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php
中找不到文件。
所以问题很明显。我希望 PHP 从应用程序的根目录(索引.php所在的位置)查找文件,但它从 config.php 所在的文件夹中查找文件。
我这个问题已经有很长时间了,但我想一劳永逸地找到解决方案。
我不能让它做我想要的!我希望有人能帮助我。
谢谢。
PS:换句话说,我想使用绝对路径包含文件。
PS:这是完整的错误文本:
Warning: include(/Lab/Livre/POO/TP1/core/application/controller_base.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/controller_base.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(/Lab/Livre/POO/TP1/core/application/registry.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/registry.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(/Lab/Livre/POO/TP1/core/application/router.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/router.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(/Lab/Livre/POO/TP1/core/application/template.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/template.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
我已经找到了解决问题的方法。我将根常量设置为:
define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
但是出了什么问题呢?