Jump to main content Jump to doc navigation

Loading MODX Externally

Other versions: 2.x (current) — Found a problem? Please edit this page or report an issue.

Loading the modX Object

Using the modX object (and all of its respective classes) is quite simple. All you need is this code:

require_once '/absolute/path/to/modx/config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError', '', '');

This will initialize the MODX object into the 'web' Context. Now, if you want to access it under a different context (and thereby changing its access permissions, policies, etc), you'll just need to change 'web' to whatever Context you want to load. This also loads the MODX error handler as well.

From there, you can use any MODX methods, functions, or classes.

Another Example

Build scripts are a great place to see MODX loaded up from the command line. They usually begin with something like this:

if (!defined('MODX_CORE_PATH')) {
       define('MODX_CORE_PATH', '/path/to/core/');
}
if (!defined('MODX_CONFIG_KEY')) {
       define('MODX_CONFIG_KEY', 'config');
}
require_once( MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx = new modX();
$modx->initialize('mgr');

Deprecated Example

This example is deprecated. So better change your code, if you still use the MODX_API_MODE.

In You can also use MODX in its API mode, and then include the primary index.php file for your site:

define('MODX_API_MODE', true);
// Full path to the index
require_once('/path/to/modx/public_html/index.php');
$modx->initialize('mgr');

See Also