Paths when both include and request AJAX
I was wondering if there is a way to make the relative path to the main folder (where index.php is located) from each file that needs to be included or requested by AJAX.
I want to combine both AJAX and PHP so that first the page is loaded with PHP and then to be able to update parts of the page with AJAX, but the files are the same and are in subfolders.
I am having problems with the path, and although I can set an absolute path, I have to change it every time the server changes. I want a relative path to my project, but not DOCUMENT_ROOT because it doesn't work with aliases. (or do you know how to make it work with aliases?)
Thanks!
EDIT * This is how my directory structure looks like:
[main_dir] /index.php - main index file
[main_dir] / phpinc - php include files - which only work with include (no AJAX call)
[main_dir] / modules - modules are parts of the page, can be included in PHP or AJAX, called from anywhere (it doesn't matter, they will be loaded as a separate HTML file, so the paths in PHP are relative to where the Module is physically located.
When you include them in PHP, they usually take the [main_dir] path (because they were included in index.php)
So my problem is, from the module files, I want to include some files in the phpinc folder, but that could be either .. /phpinc/file.php or phpinc / file.php.
Is there a more elegant way than just putting
if (is_dir("phpinc")) {
$inc_path = "phpinc";
} else if (is_dir("../phpinc")) {
$inc_path = "../phpinc";
}
at the beginning of each module?
a source to share
I would suggest using the PHP autoloader:
I created a CAutoloadManager class that has this method:
static public addRepositoryForFilePattern( String $aPath, String $sPattern ) {}
Inside index.php, I call
AutoloadManager::addRepositoryForFilePattern( './actions/', 'C*Action.inc.php' );
AutoloadManager::addRepositoryForFilePattern( './includes/', '*.inc.php' );
to instruct autoloading, first try to find a class in. / actions / for names like CShowWelcomeAction.inc.php or CLoginAction.inc.php. CAutoloadManager did a search for. / Includes / for other classes.
PHP autoload () function is used like this:
function __autoload($class_name) {
include $class_name . '.php';
}
PHP calls it every time it fails to load the class.
Actuall, this works really well - albeit for classes.
a source to share