包含来自根

时间:2016-04-24 21:22:12

标签: php file model-view-controller include

以下是我的申请结构:

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从应用程序的根目录(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

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我将ROOT常数设置为:

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

但出了什么问题?

相关问题