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 f开发者_运维技巧ile 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?
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.
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.
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.
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.
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.
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 "/".
精彩评论