The default magento-vars.php
file that is referenced through the Adobe Commerce documentation leaves a lot to be desired and can become a pain when managing stores with many websites and store views.
Luckily we can simplify this configuration using the match
implementation within PHP8.
This version of the script allows us to configure new store fronts by adding a single case to the match statement.
<?php
$host = $_SERVER['HTTP_HOST'] ?? '';
$ephemeralHostScope = fn(string $host): ?string => substr_count($host, '.') === 4 ? strtok($host, '.') : null;
$_SERVER["MAGE_RUN_TYPE"] = 'store';
$_SERVER["MAGE_RUN_CODE"] = match(true) {
$ephemeralHostScope($host) === 'us' || str_contains($host, 'example.com') => 'us',
$ephemeralHostScope($host) === 'gb' || str_contains($host, 'example.co.uk') => 'gb',
$ephemeralHostScope($host) === 'cn' || str_contains($host, 'example.cn') => 'cn',
default => 'base'
};
We are targeting two URL formats within this upgrade script.
- Custom CNAMES records that point to
prod.magentocloud.map.fastly.net
(e.g staging.example.com) - Adobe Cloud dynamic urls usually in the format of
https://<store>.<integration_env>.<region>.magentosite.cloud