2016-04-24 72 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'])); 

但是出了什麼問題?