Jump to main content Jump to doc navigation

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->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();

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

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.



$0 per month—let's make that $500!

Learn more