Jump to main content Jump to doc navigation

A namespace is an organizational unit for Components. It maps a short name to a directory on disk, which lets MODX find that Component's lexicons, manager controllers, and model packages without hard-coded paths.

Lexicons and controllers resolve through a namespace. Processors do not — see Processors for how they are located.

Usage

A manager page is identified by two query parameters: namespace (default core) and a, which carries the action. Given ?a=some/action&namespace=quip, MODX looks for the controller inside the quip namespace directory — most often core/components/quip/controllers/some/action.class.php.

If a namespace "quip" has the path /www/modx/core/components/quip/, that file resolves to /www/modx/core/components/quip/controllers/some/action.class.php.

Menu entries store the same pair: modMenu carries an action column and a namespace column (default core). There is no controller column — the 2.x modAction class and its controller field were removed in 3.0. The full resolution order lives under Controllers and Menus.

Lexicons in Namespaces

Namespaces isolate Lexicons and Lexicon Topics. To load the "comment" topic from the "quip" namespace, prefix the topic with the namespace and a colon:

$modx->lexicon->load('quip:comment');

In English this looks for lexicon/en/comment.inc.php inside the namespace path, so topics can live outside the MODX core directories and load on demand.

Bootstrapping Services

If a file named bootstrap.php sits in the namespace root, MODX includes it during initialisation. Use it to inject services into the dependency injection container.

Inside the file you have access to:

  • MODX\Revolution\modX $modx
  • array $namespace

::: warning _initNamespaces() runs early — before extension packages load, before culture initialisation, and before the OnMODXInit event. At that point $modx->lexicon is still null, and no Component models are registered yet. Register autoloaders and services here; resolve anything that depends on a lexicon or a model from OnMODXInit instead. :::

Examples

To add a PSR4 autoloader pointing to the src directory in your namespace:

\MODX\Revolution\modX::getLoader()->addPsr4('My\\Component\\', $namespace['path'] . 'src/');

To load an xPDO model:

$modx->addPackage('My\\Component\\Model', $namespace['path'] . 'src/', null, 'My\\Component\\');

To register a service into the dependency injection container:

$modx->services->add('my_service', function($c) use ($modx) {
    return new My\Component\MyService($modx);
});

(Make sure your service name is properly unique.)

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.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

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

Learn more