Development

HowtoChooseApplicationAndEnvironmentBasedOnDomain

You must first sign up to be able to contribute.

Version 3 (modified by Lloyd.Leung, 11 years ago)
--

Symfony Howto: Choose Application and Environment based on domain name

Purpose

Sometimes you want to be able to choose your environment and application based on domain name. This allows you not to have index.php, index_dev.php, backend.php, backend_dev.php, etc; but just to have one index.php controlling your project.

Code

index.php

<?php
define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));


/**
  * Check for the subdomain admin and make the application admin
  */
if ( isset($_SERVER) &&
     is_array($_SERVER) &&
     isset($_SERVER['HTTP_HOST']) &&
     preg_match('/admin\./', $_SERVER['HTTP_HOST'])
   )
{
  define('SF_APP', 'backend');
}
elseif (
         isset($_SERVER) &&
     is_array($_SERVER) &&
     isset($_SERVER['HTTP_HOST']) &&
     preg_match('/telemarket\./', $_SERVER['HTTP_HOST'])
       )
{
  define('SF_APP', 'telemarketer');
}
else
{
  define('SF_APP', 'public');
}


/**
  * Check for the subdomain dev and make the enviorment dev
  */
if ( isset($_SERVER) &&
     is_array($_SERVER) &&
     isset($_SERVER['HTTP_HOST']) &&
     preg_match('/dev\./', $_SERVER['HTTP_HOST'])
   )
{
  define('SF_ENVIRONMENT', 'dev');
  define('SF_DEBUG',       true);
}
else
{
  define('SF_ENVIRONMENT', 'prod');
  define('SF_DEBUG',       false);
}


require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');


sfContext::getInstance()->getController()->dispatch();


Usage

This one file would allow you to have 6 different domain names doing different things

Each of the sub-domains tell it which application to use and whether or not to load up the dev environment.

You can obviously add more applications and environments to the logic statements to suit your needs.

Warning

You should make sure you either disable the dev domain names using a .htaccess file or not use this index.php file on production machines.