First, you’ll want to get the latest, as I made some significant improvements to context switching and cross-context URL generation in the core this week.
But let’s start with an example. Here is a plugin I’m using to test a single configuration that is serving two domains based on the URL used to access the site:
<?php
switch ($modx->config['http_host']) {
case 'domain2.tld:80':
case 'domain2.tld':
// if the http_host is of a specific domain, switch the context
$modx->switchContext('domain2');
break;
default:
// by default, don't do anything
break;
}
?>
So to test on my local machine, I can use localhost for web (the default context) and 127.0.0.1 to switch to my test context:
<?php
switch ($modx->config['http_host']) {
case '127.0.0.1:80':
case '127.0.0.1':
// if the http_host is of a specific domain, switch the context
$modx->switchContext('test');
break;
default:
// by default, don't do anything
break;
}
?>
You’ll want to specifically set context configuration settings in each context so URLs are generated properly from other contexts, especially the http_host and site_url settings, though you can override many others.