0

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
tomfl
  • 707
  • 1
  • 11
  • 29
  • Can you copy us the full error text? – olibiaz Apr 24 '16 at 21:25
  • Done ! Sorry, I forgot to do it. – tomfl Apr 24 '16 at 21:27
  • 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 Apr 24 '16 at 21:30
  • Did you try `dirname(__FILE__) . ROOT . '/core/application/template.class.php'` – olibiaz Apr 24 '16 at 21:36
  • Thanks for the answers ! I will try all of this tomorrow as it is midnight in France. – tomfl Apr 24 '16 at 22:19
  • Hello again, i tried to do that but and here is a part of what I get : `Warning: include(/Users/tom/www/Lab/Livre/POO/TP1/core/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`. So it's the same type of error. If you want to know more about what I'm trying to do, I'm actally following this tutorial : http://www.phpro.org/tutorials/Model-View-Controller-MVC.html – tomfl Apr 25 '16 at 07:12
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Apr 26 '16 at 12:24

1 Answers1

0

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 ?

tomfl
  • 707
  • 1
  • 11
  • 29