0
votes

Here is the structure of my application :

core
    application
        controller_base.class.php
        registry.class.php
        router.class.php
        template.class.php
    controller
    include
        config.php
    model
    views
index.php

The file config.php is included in the index.php file (there's no problem with this) and here is what it contains :

<?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;

You have to know that the constant ROOT is defined like this :

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

This constant contains the value /Lab/Livre/POO/TP1 which is of course the root of the applications.

But there's a problem whith all the files I include in config.php. PHP throws me an error that says the files are not found in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php.

So the problem is clear. I want PHP to look for the files from the root of the application (where index.php is), but it looks for it from the folder where config.php is located.

I had this problem for a long time but I want to find a solution once and for all.

I can't get it to do want I want ! I hope someone will help me.
Thank you very much.

P.S.: In other words, i want to include the files using an absolute path.

P.S.: Here is the full error text :

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
Can you copy us the full error text? - olibiaz
Done ! Sorry, I forgot to do it. - tomfl
So php doesn't try to load the required file in the same folder that config.php. It doesn't found the files in /Lab/Livre/POO/TP1/core/application/. - olibiaz
Did you try dirname(__FILE__) . ROOT . '/core/application/template.class.php' - olibiaz
Thanks for the answers ! I will try all of this tomorrow as it is midnight in France. - tomfl

1 Answers

0
votes

I have found a solution to my problem. I set the ROOT constant to this :

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

But what went wrong ?