Reputation: 893
I have a small project but i guess this applies to any size. Since i just have 2 classes i think autoload is probably overkill and probably not too relevant here.
What i am trying to achieve is that if i need to transfer this to another server or folder that it will be compatible and wont need me having to edit a bunch of links throughout the site to get it working.
So i have a file with constants called config.php which i put in the classes folder
define('CONST_INT_PATH', $_SERVER['DOCUMENT_ROOT']);
define('CONST_ROOT_PATH', 'http://www.mysite.com');
define('CONST_PROJECT_PATH', CONST_ROOT_PATH.'/website');
define("CONST_IMAGE_PATH", CONST_PROJECT_PATH.'/images/');
define("CONST_CLASSES_PATH", CONST_PROJECT_PATH.'/classes/');
define("CONST_INCLUDE_PATH", CONST_PROJECT_PATH.'/includes/');
define("CONST_JS_PATH", CONST_PROJECT_PATH.'/js/');
define("CONST_CSS_PATH", CONST_PROJECT_PATH.'/css/');
So the root of the project i.e var/www is equivilant to www.mysite.com and the subfolder of where the website is var/www/website i.e www.mysite.com/website
To be honest im a bit lost, in some parts using CONST_INT_PATH works fine, other places i have to use CONST_PROJECT_PATH and use the full url path. I think the latter is possibly the better option but i am not sure.
Is there a better way to do this that works in all cases
i use it something like this now:
include CONST_INCLUDE_PATH.'header.php';
include CONST_INCLUDE_PATH.'footer.php';
<img src="<?php echo CONST_PROJECT_PATH;?>logo.png" />
So my question is which one of these is the right one to use or is there a better method
define('CONST_INT_PATH', $_SERVER['DOCUMENT_ROOT']);
define('CONST_ROOT_PATH', 'http://www.mysite.com');
define('CONST_PROJECT_PATH', CONST_ROOT_PATH.'/website');
I'm seeing weird issues with includes not loading even though if i echo out the include and paste in the url i can see it so im hoping it might be because the method im currently using is flawed
For example if i use:
include 'includes/header.php';
that works fine but if i do:
include 'http://www.mywebsite.com/website/includes/header.php';
it wont work even though i know that is the correct path. So i'm guessing i need to drop the http:// route and get to the path a different way
Upvotes: 0
Views: 53
Reputation: 893
Ok so i got it working, this method might not achieve what i want long term but it seems to be working. I dont quite understand fully why yet.
If i use this in my config file:
define('CONST_ROOT_PATH', $_SERVER['REQUEST_URI']);
define('CONST_PROJECT_PATH', dirname(__FILE__));
define("CONST_IMAGE_PATH", CONST_ROOT_PATH.'/images');
define("CONST_CLASSES_PATH", CONST_PROJECT_PATH.'/classes');
define("CONST_INCLUDE_PATH", CONST_PROJECT_PATH.'/includes');
define("CONST_JS_PATH", CONST_ROOT_PATH.'/js');
define("CONST_CSS_PATH", CONST_ROOT_PATH.'/css');
The weird thing is that the CONST_PROJECT_PATH works fine for classes and includes but it does not work for js and css files for some reason. even though all those folders are in the same directory, using CONST_ROOT_PATH which uses $_SERVER['REQUEST_URI'] makes the css and js work, that basically returns /website instead of var/www/website/
Upvotes: 1
Reputation: 33
No, the reason we have configuration files is so that we can edit only that one file when transferring code across different environment. Set the Doc root and all file paths should go from that setting. Then all you need to do is change the doc root setting depending on the environment.
Upvotes: 0