Overview
One of the configuration strategies for Magento multisite entails modifying the main index.php file so that it:
Original index.php
The original index.php file ends with a section like this:
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
|
A key point to note is that the $bootstrap object gets passed the standard php $_SERVER variable which is an array containing name-value pairs from the invoking environment, such as HTTP_HOST and any environment variables. Those variables would include MAGE_RUN_CODE and MAGE_RUN_TYPE, if you had set them using one of the other methods, and this is the means by which the choice of Magento sites gets conveyed into Magento.
Revised index.php
Here we'll assume that MAGE_RUN_CODE and TYPE environment variables have not been set upstream of invocation of index.php. So the gist of the method described here is to add some additional code in index.php to set them. (If they have been set upstream, those values will be overwritten here.)
Here's an example:
$params = $_SERVER;
switch($_SERVER['HTTP_HOST']) {
case 'furniture.yoursite.com':
$params['MAGE_RUN_CODE'] = 'furnx';
$params['MAGE_RUN_TYPE'] = 'website';
break;
case 'sportswear.yoursite.com':
$params['MAGE_RUN_CODE'] = 'sportsw';
$params['MAGE_RUN_TYPE'] = 'website';
break;
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
|
The code
- creates a copy of $_SERVER in new variable $params.
- detects which site was requested
- inserts or overwrites into $params the name-value pairs MAGE_RUN_CODE and MAGE_RUN_TYPE
- continues as in the original code to launch the rest of Magento, but this time passing the modified set of parameters ($params).
Complicated-looking version explained
The code as shown above is a reasonable solution, however you sometimes see it written in a more complicated fashion, which bears explaining.
$params = $_SERVER;
switch($_SERVER['HTTP_HOST']) {
case 'furniture.yoursite.com':
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'furnx';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
break;
case 'sportswear.yoursite.com':
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'sportsw';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
break;
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
|
This example differs from the previous in only one respect: The $param array indexes 'MAGE_RUN_CODE' and 'MAGE_RUN_CODE' have been replaced by:
These are simply constant strings defined in file ...\vendor\magento\module-store\Model\StoreManager.php, and of course those constant values are 'MAGE_RUN_CODE' and 'MAGE_RUN_CODE' respectively. So the effect is identical to the previous example code.
The appearance of these PARAM_RUN_CODE and TYPE "things" has created some confusion in the docs, forums etc, partly because they are named conspicuously different than MAGE_RUN_CODE and MAGE_RUN_TYPE. There's no particularly good reason these constants are named differently; the important part is that they refer to the same old 'MAGE_RUN_CODE' and 'MAGE_RUN_TYPE' strings.