cliveportman/code

Craft CMS routes.php environmental config

The routes.php config file does not support environmental variables like general.php and db.php so the following will not work:

return array(
    '*' => array(
    ),
    'jobs.website.com' => array(
         'account/login' => '_jobs/account/login',
         'account/created' => '_jobs/account/created',
         'create' => '_jobs/create/index',
         'edit/([^/]+)' => '_jobs/create/edit',
         'copy/([^/]+)' => '_jobs/create/copy',
         'delete/([^/]+)' => '_jobs/create/delete',
         'cart' => '_jobs/checkout/index'
    )
);

But as it's a PHP file there's a workaround like so:

$routes = array();
$jobsRoutes = array(
    'account/login' => '_jobs/account/login',
    'account/created' => '_jobs/account/created',
    'create' => '_jobs/create/index',
    'edit/([^/]+)' => '_jobs/create/edit',
    'copy/([^/]+)' => '_jobs/create/copy',
    'delete/([^/]+)' => '_jobs/create/delete',
    'cart' => '_jobs/checkout/index' 
);
if (strpos(CRAFT_ENVIRONMENT, 'jobs.website.com') !== false) {
    $routes = array_merge($routes, $jobsRoutes);
}
return $routes;

Hat-tip.