I ran across this issue while wrapping a third-party script in MODx templates, but it applies to any usage of MODx in API mode.
Test case
MODx is installed at [tt]htdocs/index.php[/tt]. I create a subfolder called news, and add a PHP file: [tt]htdocs/news/index.php[/tt]. In this file I add the following:
<?php
define('MODX_API_MODE', true);
include($_SERVER['DOCUMENT_ROOT'].'/index.php');
$modx->db->connect(); // provide the MODx DBAPI
$modx->getSettings(); // provide the $modx->documentMap and user settings
print MODX_BASE_PATH ."<br>\n";
print MODX_BASE_URL ."<br>\n";
print MODX_SITE_URL ."<br>\n";
If you run the file, you will get output like:
/var/www/modx/htdocs/
/news/
http://modx/news/
Now I can’t think of a scenario where this is the expected behaviour. Is there a reason for it, other than an issue with the code? Because the MODx install is in the parent directory, and so the URLs (used for the <base> tag and menus) are wrong.
Although it looks like people have
worked around this problem by using the external MODxAPI class it doesn’t solve the problem when code is placed in a subdirectory. I feel this is something that should be addressed internally in MODx core. What does the community think?