Absolute vs. relative paths
If I use absolute paths, I can't move the whole directory to a new location. If I use relative paths, I can't move individual files to new locations.
What's the solution here? Do you set up a config file that holds the root path and go from there? Or do you have a rule like: Never move files around?
I've seen in some projects that people use dirname(FILE). What is the point of that, I mean, why not simply leave it out since the dirname is relative anyway (depending on where the file sits)?
Solution 1:
you should use a config file that will be included in each file first line, for example your app look like this
root / App / Plugins
inside your root dir : app-config.php
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
now, suppose you have to include a plugin file, so
inside your Plugin dir : my-plugin.php
require_once '../../app-config.php';
now everything below this line can use ABSPATH
example do you want to load an image
<img src='".ABSPATH."Public/images/demo.png' alt=''/>
now, the thing is more simple if your app is designed to automatically load some files like
plugin-widget-1.php
so that everything inside this file or any other file loaded by the my-plugin.php
file can use the ABSPATH
without include each time the app-config.php
file.
with this in mind you can have all the short-hand you want into the app-config.php
example
define('UPLOAD_PATH', ABSPATH. 'Public/uploads/');
define('IMAGES_PATH', ABSPATH. 'Public/images/');
define('HELPERS_PATH', ABSPATH. 'App/helpers/');
...
so, now that you have all defined, if you need to move a file, let's say one folder forward example:
root / App / Plugins / Utils
just inlucde require_once '../../../app-config.php';
obviously i suppose that you are not changing paths each time =) anyway if you need to do so is always more simple to change one file inclusion instead of hundreds.
hope this make sense to you =)
Solution 2:
I've seen in some projects that people use dirname(FILE). What is the point of that, I mean, why not simply leave it out since the dirname is relative anyway (depending on where the file sits)?
It's relative to the include path, anyway. The dirname( __FILE__ )
(or just __DIR__
in PHP >= 5.3) is there so you can run the file from every location. In case you're using relative paths, the value "." may change. See:
berry@berry-pc:~% cat so.php
<?php
var_dump( realpath( '.' ) );
var_dump( realpath( __DIR__ ) );
berry@berry-pc:~% php so.php
string(11) "/home/berry"
string(11) "/home/berry"
berry@berry-pc:~% cd foo
berry@berry-pc:~/foo% php ../so.php
string(15) "/home/berry/foo"
string(11) "/home/berry"
So, it is relative, but it's relative to the current working directory, not to the directory the file is located in. That's why you'll want to use __DIR__
for this. And by the way; yes, I don't move files around an awful lot. If I do though, I'll have to update every call to that file, although I don't require or include an awful lot anymore, since I'm using an Autoloader.
As for the other files I'm referring to (such as template files), I set the path manually, but once. I then refer to $path . '/filename.php';
, so it's easier to change later.
Solution 3:
Depends on your usage, define a constant to hold application path is one of the common practice
Don't use variable as it might get override somewhere in your application and can break your application
Is even better to combine with symlink (symbolic to create branches)
application dirs
===============================
/home/latest -> symlink
/home/testing -> symlink
/home/20111202000000 -> latest development version
/home/20111201000000 -> yesterday stable release
So, you can symbolic link
/home/testing -> /home/20111202000000 while keeping the stable version,
/home/latest -> /home/20111201000000
With this, you don't have to risk to break your production site while doing some testing/development, and, you can easily shift your development directory