tags:

views:

250

answers:

6

I need 2 different paths, one for includes and one for js/css etc. I'm using mod_rewrite. The below works fine....

Currently all my files contain this at the top

define('SERVER_ROOT',   'C:/wamp/www/site_folder/');
define('SITE_ROOT',     'http://localhost/site_folder/');

and then files are called like so:

require_once SERVER_ROOT . 'st_wd_assets/inc/func_st_wd.php';    

<link rel="stylesheet" type="text/css" href="<?php echo SITE_ROOT;?>st_pages/user_area/css/user_area.css" media="screen"/>

as you can probably see, it's going to be a massive chore to update the top of every file everytime i move versions between the localhost and my live server.

What's the best/standard way of defining these ROOT values?

I can't see a solution in the $_SERVER super global? Do people normally just use VirtualHosts? But then wouldn't it still be necessary to define ROOT constants?

+1  A: 

Yes. People normally just use VirtualHosts.

There are several ways.

  • You can use a relative path to include a config file.
  • You can use a DOCUMENT_ROOT from the $_SERVER superglobal to place a config file there.
  • You can use web-server config if possible. like php_value auto_prepend_file in .htaccess
  • And at least you can detect your environment and choose between two roots, both written in conditions at the top.
  • And yes, if you're using mod_rewrite - make a front controller which will include all the other files, so - the only one file to place these settings.
Col. Shrapnel
i forgot about auto_prepend_file, could be easiest method - any downsides?
Haroldo
it works only with apache and php as the module - the only known to me limitation. I use it pretty often
Col. Shrapnel
thanks for your help on this Col. Shrapnel, much appreciated
Haroldo
A: 

With a combination of relative includes/requires and autoloading of classes you can avoid having a SERVER_ROOT altogether. Something like your SITE_ROOT belongs in a config file that is included where needed.

Iggy Kay
Autoloading is expensive.
Sepehr Lajevardi
plus relative paths get messed up my mod_rewrite
Haroldo
A: 

If you're not using VirtualHosts then $_SERVER['DOCUMENT_ROOT'] may not return the correct value. For your site it looks like it will return "C:/wamp/www"

You could use this simple define to set SERVER_ROOT as the current directory followed by a slash (directory separator):

define('SERVER_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

This should then define SERVER_ROOT as "C:/wamp/www/site_folder/" for your server.

However, this probably won't work unless all your .php files are contained within "C:/wamp/www/site_folder/". The define will return different values for .php files which are in subdirectorie of that folder.

it is called relative path and cannot be used with complex directory structure.
Col. Shrapnel
Well, he did state he was using mod_rewrite. In which case his rewrites may only point to files in C:/wamp/www/site_folder and not any subdirectories.
it's also likely that the site will neeed to be deployed to different folders, ie mydomain.com/test then if that works, mydomain.com
Haroldo
Yes, the code i've posted here means that the value changes automatically regardless of what folder your files are in.
__FILE__ gives the path of the file name and not the root? how is that useful in a non Controller based setup?
Haroldo
What does your file/directory structure look like? (And how are your mod_rewrites setup?)
A: 

I use php_uname('n') which returns the name of the computer that php is currently running on. With this, you can make per-computer config files:

$configFile = 'config/' . php_uname('n') . '.config.php';

if (file_exists($configFile)) {
    include($configFile);
}

Then you put the configuration directives in config/your_computer_name.config.php, and config/live_servers_name.config.php.

WishCow
this looks smart, does anyone have any thoughts on this method?
Haroldo
@Haroldo The method itself is listed in my answer in the 4th item. Not as good as having these defines in the single config file. And the implementation shown here as bad as other relative paths solutions.
Col. Shrapnel
A: 

Create one file in your docroot. Call it settings.inc.

//settings.inc:

define('SITE_ROOT',     'site_folder');
require_once (dirname(__FILE__)) . '/st_wd_assets/inc/func_st_wd.php';

Then in each of your files,

require_once (dirname(__FILE__)) . "/settings.inc";

Don't worry about the root and whatnot for css or hyperlinks. Just preface them with a "/".

Entendu
`dirname(__FILE__)` is senseless construct. if all files take place in the same directory, it is useless, and you can write just `require_once "settings.inc";` As for the other directories it just wouldn't work.
Col. Shrapnel
and his site root is not just / but /test/. That's silly but that's a fact.
Col. Shrapnel
my root is / but i'll need to test and get feedback it somewhere, hence /test/, beta.mydomain or similar
Haroldo
@Haroldo your root is not / but /site_folder/ that's the only reason you were asking your question.
Col. Shrapnel
i'm asking these questions becuase my root will change depending on whether i'm on a) local machine b)test server c) live site
Haroldo
A: 

why not to make config.php file with

define('SERVER_ROOT',   'C:/wamp/www/site_folder/');
define('SITE_ROOT',     'http://localhost/site_folder/');

and not appending it at the top of every file.

require_once ('config.php');

once made - no problems in future. If eou are moving your site - simply edit or not overwrite config file.

GOsha