2016-04-24 44 views
0

以下文件是我的应用程序的结构:包括从根

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; 

你要知道,恒根源是这样定义的:

define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME'])); 

这个常量ant包含值/Lab/Livre/POO/TP1,这当然是应用程序的根。

但是,在config.php中包含的所有文件都存在问题。 PHP会抛出一个错误,说明在/Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php中找不到这些文件。

所以问题很明显。我希望PHP从应用程序的根目录(其中index.php是)查找文件,但它会从config.php所在的文件夹中查找它。

我有这个问题很长一段时间,但我想找到一个解决方案。

我不能让它做我想要的!我希望有人能帮助我。
非常感谢。

P.S .:换句话说,我想包括使用绝对路径的文件。

P.S:以下是完整的错误文本:

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 
+0

你能复制我们完整的错误文本吗? – olibiaz

+0

完成!对不起,我忘了这么做。 – tomfl

+0

因此,php不会尝试在config.php所在的文件夹中加载所需的文件。它没有在'/ Lab/Livre/POO/TP1/core/application /'中找到这些文件。 – olibiaz

回答

0

我已经找到了解决我的问题。我将ROOT固定为:

define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME'])); 

但是出了什么问题?