There has been some incivility here which prevented @genesis to get what he needs.
Because I pretty much "need" the same, here is my (reformulated) question (and a potential answer).
Assumptions: Application can be installed pretty much anywhere, since it will be downloaded by someone from an open-source repository. So there is the need to automatically determine:
- Absolute path to install directory (e.g.
/var/www/myserver/mysubdir/
) which will be required for include files;
- Absolute URL for that very same directory, for instance, for menu navigation and calling .php files from anywhere within the application (e.g.
http://myserver.tld/mysubdir/
)
- There might be an unknown depth limit to those subdirectories: for example, under the main directory, there might be
/css
, /js
but also things like /admin/plugins/js
which, however, might need to retrieve a config.php
from the top directory to know about things like database connection strings, global app parameters, etc.
- The application itself might use a few levels by itself; the user who installs the application might also do the same (e.g.
/var/www/myserver
is where the virtual host is pointing to, but the end-user wants the application to be under /var/www/myserver/yes/I/really/want/it/this/deep
and expects the application to be fully functional at http://myserver.tld/yes/I/really/want/it/this/deep
I have taken a look at how WordPress does this. Basically, WordPress requires that the URL pointing to the directory structure is hard-coded by the user (other applications do not). Then it extracts the directory path (for file inclusion) with
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
This looks rather neat, but, in reality, the problem is that if you're "deep" in the directory hierarchy, it might be impossible to include the file that defines ABSPATH. What WordPress is constantly doing is to call those two lines and include the required configuration file after it made sure that it got a valid ABSPATH.
But other software seem to be much better organized. They automagically extract the correct URL to the directory where the webserver is pointing to; and they even-more-magically are able to find where all the include files are, no matter how deep in the subdirectory structure they're located.
The best I could do is to emulate WordPress: 'force' the user to write the URL in the configuration file, and, on all levels and sublevels, start pretty much every file with
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
and attempt to include the global configuration file by guesswork (e.g. test if it's at the same level, one level up, two levels up, and so forth...).
This is extremely messy, but it seems that's what some gold-class applications (like WordPress) are doing. Others seems so much more organized. How do they do it? I have no idea.
@genesis, is this what you had in mind?