0

When developing my website I called all the includes in my php files by calling one single file called includes.

The code of this file looked somethig like this: (I adapted it from a Lynda tutorial)

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
    define('SITE_ROOT', 'C:'.DS.'wamp'.DS.'www'.DS.'ArmyCreator');

defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
defined('PUB_PATH') ? null : define('PUB_PATH', SITE_ROOT.DS.'public');


// load config file first
require_once(LIB_PATH.DS."helper".DS.'config.php');

Now since I am deploying my webiste I can't figure out how to declare the SITE_ROOt to make it work properly?

EDIT

Is it normal for code like this: require_once("../../includes/helper/initialize.php"); to not work anymore once I deploy to the website?

Pillblast
  • 1,141
  • 5
  • 18
  • 25

4 Answers4

6

You can include the files relative to includes.php's directory by doing:

<?
$basePath = dirname(__FILE__);
require_once($basePath . "relative/path/from/basePath"); // replace the string with your real relative path
Kyle Wild
  • 8,625
  • 2
  • 34
  • 36
1

Two suggestions here:

  1. You're going to want SITE_ROOT to be absolute path of the directory your files are located in. For example, in the above code, this directory is C:\wamp\www\ArmyCreator. You can define this manually if you know the directory, or dynamically by using the predefined __DIR__ constant (PHP 5.3+), or dirname(__FILE__) if you're not on 5.3 yet.

  2. Including a bunch of files all at once in generally considered bad practice, and autoloading should be used instead. This will give you better performance as well as a well-defined directory layout and naming scheme, leading to better code. To do this, you can use the spl_autoload_register() function.

mfonda
  • 7,663
  • 1
  • 25
  • 30
1

First off, don't abuse ternary syntax like that. Instead of defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);, you can use the OR operator (which will short-circuit on a boolean true result):

defined('DS') OR define('DS', DIRECTORY_SEPARATOR);

Secondly, if this is inside of a bootstrap file that you know the position of, simply use dirname(__FILE__):

defined('SITE_ROOT') OR define('SITE_ROOT', dirname(__FILE__));

Otherwise, if you know the relative position of the root, you can use multiple dirname calls. So if it's the parent directory of the present one:

defined('SITE_ROOT') OR define('SITE_ROOT', dirname(dirname(__FILE__)));

Don't use $_SERVER['DOCUMENT_ROOT'] or cwd() or hardcode your path. Always use dirname(__FILE__) to determine the absolute path. For more info on why, see This Answer

Community
  • 1
  • 1
ircmaxell
  • 159,979
  • 33
  • 259
  • 312
  • Even cleaner than `or` would be `if (!defined('...')) { define('...') }` – mfonda Jan 19 '11 at 16:37
  • Quite true @mfonda, but there's a line between verbosity and clutter. Having a whole bunch of `if` statements (taking between 2 and 3 lines each, since I don't know of a single coding standard that allows inline-if statements) is more clutter than it may be worth... But to each their own... – ircmaxell Jan 19 '11 at 16:40
0

First off: I'd drop the DS, it's BS (ehe). Windows support both C:/wamp/www and C:\wamp\www :-) Even C:\wamp\www/project is fine.

If includes.php is located in, say lib/includes.php (relative to your project root), then do this:

define('SITE_ROOT', realpath('../'));

That will dynamically set SITE_ROOT.

Znarkus
  • 22,406
  • 22
  • 77
  • 108
  • The `DS` is not BS. Sure, windows supports both path syntaxes, but it's ambiguous and will not match the result of `realpath()`. I do consider it to be very bad practice to blindly use `/` on windows systems. But that's just my $0.02... – ircmaxell Jan 19 '11 at 16:37