1

Is there a favored method for specifying the root folder of your website/application without having to resort to code like this:

require '../../../../config/settings.php';

I've used various methods before, and the one I'm currently considering is setting an environment variable on my host, so that I can do something like this instead:

require getenv('PROJECT_ROOT') . '/config/settings.php';

But this is really only feasible for systems with one project on them, and it has various other disadvantages, so I was wondering if there was a better method?

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • 3
    There's also the `include_path`. But why not set such an environment variable? It can be done per-directory, thus per-project. – mario Jan 31 '12 at 22:35
  • 1
    Possible duplicate: http://stackoverflow.com/questions/1755216/php-relative-and-absolute-paths – Ben Brocka Jan 31 '12 at 22:53
  • Env's are actually my favorite since I had issues on a server where `$_SERVER['DOCUMENT_ROOT'];` was not set due to virtual doc roots... – Wrikken Jan 31 '12 at 23:00
  • @mario: Are you talking about Jim OHalloran's answer? – FtDRbwLXw6 Jan 31 '12 at 23:42

4 Answers4

3

If you wanted to go down the environment variables path, you are able to set environment variables from your Apache vhost. Check out SetEnv and SetEnvIf in the Apache docs. e.g. To supply an environment variable to all incoming requests for a given apache vhost:

SetEnv PROJECT_ROOT='/path/to/my/project'

e.g. To change the value of an environment variable based on host name:

SetEnvIf Host www.example.com PROJECT_ROOT='/path/to/example/com'
SetEnvIf Host www.example.org PROJECT_ROOT='/path/to/example/org'

Other options include autoloading and bootstrapping. Autoloading works great for classes, but not for config files like your example. A good naming convention (such as PSR-0) will make mapping class names to file names much easier. Bootstrapping involves creating a file which you include from all of your scripts which in turn loads things like configuration settings. The bootstrap is a great place to set up autoloading. That way your bootstrap loads the configuration before the rest of the app is loaded, and you just assume it's present elsewhere in your app.

Jim OHalloran
  • 5,859
  • 2
  • 37
  • 57
  • I guess the problem with bootstrap files is that you still have to include it, so you still have the same issue - `require ../../../../bootstrap.php`. – FtDRbwLXw6 Jan 31 '12 at 23:39
  • You still need to include/require it somehow yes. You could set the application root via an environment variable to tidy up the include statement. – Jim OHalloran Feb 01 '12 at 01:05
0

Maybe you can use following scripts:

$_SERVER['DOCUMENT_ROOT'];
$currentDirectory = array_pop(explode("/", getcwd()));

But also better than that you can refer to this question: How can I get the "application root" of my URL from PHP?

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349
0

You may want to use a bootstrap to load the necessary files you need so that successive files will only have to require the bootstrap file.

mdprotacio
  • 842
  • 6
  • 18
0

Without knowing how your code is organized, I would say look into using PHP's autoloading functionality. (http://php.net/manual/en/language.oop5.autoload.php) Provided you follow the right conventions you can programmatically map the class name to the path to load it from. Or to traverse up the directory tree until a match is found.