Dynamic absolute path for includes/requires
Posted: Sun May 26, 2013 6:12 am
I've made my case a few times to theme makers that prefer to split off parts of pages and call them with their own
calls
But that breaks it when using vQmod because it assumes the relative path. And since vQmod changed files run out of the vqcache path, the solution is to use the full absolute path to where it "should" be, but to be sure it is dynamic per site.
I always used the following:
as that will use the DIR_SYSTEM path which is guaranteed to exist and be constant among all opencart stores.
I include the '../' to tell the code to go up one level which is the parent directory of the "system" folder
That is the guaranteed way to find the root folder of the cart. From there you can build the path to your file. Alternatively you can use:
which functionally does the same thing.
But aside from making it work with vQmod, it also has its other benefits from a code perspective shown here:
http://www.kavoir.com/2010/02/php-why-y ... e-php.html
Read down to the "a better approach" part....
So not only does it make it vQmod compatible, it also speeds things up a tad
Code: Select all
include('xxx.php');
But that breaks it when using vQmod because it assumes the relative path. And since vQmod changed files run out of the vqcache path, the solution is to use the full absolute path to where it "should" be, but to be sure it is dynamic per site.
I always used the following:
Code: Select all
include(DIR_SYSTEM . '../path/to/file/xxx.php');
I include the '../' to tell the code to go up one level which is the parent directory of the "system" folder
That is the guaranteed way to find the root folder of the cart. From there you can build the path to your file. Alternatively you can use:
Code: Select all
include(dirname(DIR_SYSTEM) . '/path/to/file/xxx.php');
But aside from making it work with vQmod, it also has its other benefits from a code perspective shown here:
http://www.kavoir.com/2010/02/php-why-y ... e-php.html
Read down to the "a better approach" part....
So not only does it make it vQmod compatible, it also speeds things up a tad