Search code examples
phpinclude-path

How do you developers set your paths for templates?


I thought I would ask in case I could do it a better way.

On my local (WAMP) I have all my website in the www folder. ( C:\wamp\www )

Now currently I do this when i include a file:

require_once("".$_SERVER['DOCUMENT_ROOT']."/lib/config.php");

When I am working on local and upload site to a webhost i want to ensure the paths don't breakI

Could someone please tell me if I should be doing it this way?

I want to ensure maximum compatibility; meaning that paths won't break if I, for example, move site from local to whatever web host I decided to use or if I, for example, move from one host to another.

Maybe there is a more bullet proof way of doing it?


Solution

  • What I usually do, is make 1 config file (which might include others) with a few very basic constants:

    define('PROJECT_ROOT', dirname(dirname(__FILE__))); // or dirname(__DIR__) for PHP 5.3
    define('PROJECT_WEB', $_SERVER['DOCUMENT_ROOT']);
    // etc
    

    Al my other files/includes will be based on those very simple constants. I will never need relative paths and never the include_path, because both PROJECT_ROOT and PROJECT_WEB are 'real'/absolute.

    Other useful (?) constants would be PROJECT_LOGIC and/or PROJECT_CONTROLLERS and/or PROJECT_3RD_PARTY etc.