Created
November 10, 2017 21:41
-
-
Save ryanscherler/c32a7db09906049129a842b63693ccef to your computer and use it in GitHub Desktop.
Craft 3 Multisite dynamic mapping based on .env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Craft web bootstrap file | |
*/ | |
// Set path constants | |
define('CRAFT_BASE_PATH', dirname(__DIR__)); | |
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor'); | |
// Load Composer's autoloader | |
require_once CRAFT_VENDOR_PATH.'/autoload.php'; | |
// Load dotenv? | |
if (file_exists(CRAFT_BASE_PATH.'/.env')) { | |
(new Dotenv\Dotenv(CRAFT_BASE_PATH))->load(); | |
} | |
// Multisite dynamic mapping based on server name defined in `.env` | |
// Example `.env`: SITES="site1.dev:site1Handle,site2.dev:site2Handle" | |
if (getenv('SITES')) { | |
$sites = explode(',', getenv('SITES')); | |
$siteMappings = []; | |
foreach($sites as $site) { | |
if (strstr($site, ':')) { | |
$mapping = explode(':', $site); | |
$siteMappings[$mapping[0]] = $mapping[1]; | |
} | |
} | |
if (isset($siteMappings[$_SERVER['SERVER_NAME']])) { | |
define('CRAFT_SITE', $siteMappings[$_SERVER['SERVER_NAME']]); | |
} | |
} | |
// Load and run Craft | |
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production'); | |
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php'; | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment