Search code examples
phpcontent-management-systeminclude-path

PHP Include Path CMS


I have a problem with my include path. I am trying to build a cms but I want to build it so that not all the files need to be in the root for the including.

Link To File Structure

I am trying to include from anywhere to an other file. But the problem is that I include files in other files.

Like: I include ErrorHandling in T_ErrorLog But i use A textfile in Textfiles in ErrorHandling so that path isn't correct.

Errors:

Notice: Undefined variable: error in C:\Users\Kevin\Desktop\Dropbox\Programming\Webdesign\Workspace\KLJ Peer\GhostCMS\TestFiles\T_ErrorLog.php on line 7
Test

Warning: fopen(/GhostCMS/Textfiles/ErrorLog.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Users\Kevin\Desktop\Dropbox\Programming\Webdesign\Workspace\KLJ Peer\GhostCMS\Core\ErrorHandling.php on line 15
can't open file

T_ErrorLog.php

<?php

include_once ('../Core/ErrorHandling.php');

use Core\ErrorHandling;

ErrorHandling::HandleError("Test", $error);

ErrorHandling.php

namespace Core;

class ErrorHandling {
    public static function HandleError($errorMessage, $error){
        echo $errorMessage . "<br />";
        echo "Indien dit zicht blijft voor doen.<br />";
        echo "Zal de webmaster dit ontvangen en even kijken naar het probleem!<br />";

        ErrorHandling::WriteToErrorFile($errorMessage, $error);
    }
    public static function WriteToErrorFile($errorMessage, $error){
        $myFile = "/GhostCMS/Textfiles/ErrorLog.txt";
        $fh = fopen($myFile, 'a') or die("can't open file");

        $today = date('j/n/Y - H:i:s');
        fwrite($fh, "Error: \n");
        fwrite($fh, "Date: " . $today . "\n");
        fwrite($fh, "User error message: " . $errorMessage . "\n");
        fwrite($fh, "System error message: " . $error . "\n");
        fwrite($fh, "--------------------------------------------------\n");
        fclose($fh);
    }
}

Is there some way to make the include path relative for al the directories???


Solution

  • Well you know your file structure and it won't really change. Generally speaking you have a few options: record the root directory or starting point from where to derive your other files to include with options like DIR or FILE PHP Magic Constants

    e.g. you could have saved FILE upon install or in your bootstrap file and then just adapt directories to derive from that path.

    $myPreviouslySavedPath = __DIR__   // e.g. /home/user/meme/www/
    fopen($myPreviouslySavedPath . '/GhostCMS/Textfiles/ErrorLog.txt') // would then be /home/user/meme/www/GhostCMS/Textfiles/ErrorLog.txt 
    

    Alternatively for php includes php has an autoloader, which is just a function you supply yourself, where you let that function decide where to load the specific file from. This is something you'd set in the entrypoint of your CMS and would probably combine with the base directory you collected.

    e.g.

    spl_autoload_register('myClassLoader');
    
    function myClassLoader($class){
         // << here you'd place logic to decide the best path for your file >>
         include(__DIR__ . "/classes/$class");
    }
    

    The main thing to remember and use is that you have a fixed entrypoint for your application and you know the location of that file. Therefrom you can deduce the logical path of all other files.