Serving multiple Symfony apps from one project with one vhost
I have created this simple three step approach for a Symfony project to switch application contexts while respecting the desire to have a global project-wide config file, vhost and default mod_rewrite configuration.
1. /config.php:
define('URI_ROOT', '/project/web/');
define('MY_ENV', 'dev');
define('MY_DEBUG', true);
$apps = array(
'default' => 'frontend',
'admin' => 'backend'
);
2. /myboot.php
// adjusts SERVER REQUEST URI value
// if necessary and sets current app constant
function routeApp($apps)
{
$alt_app = '';
if(isset($_SERVER['REQUEST_URI']))
{
foreach($apps as $alias => $app)
{
if($alias != 'default')
{
$alias_pattern = URI_ROOT.$alias;
if(strpos($_SERVER['REQUEST_URI'], $alias_pattern) === 0)
{
$alias_preg_pattern = '/^'.str_replace('/',
'\\/', $alias_pattern).'/';
$_SERVER['REQUEST_URI'] =
preg_replace($alias_preg_pattern, URI_ROOT.'',
$_SERVER['REQUEST_URI']);
$alt_app = $app;
break;
}
}
}
}
if($alt_app != '')
{
define('MY_APP', $alt_app);
}
else
{
define('MY_APP', $apps['default']);
}
}
3. '/web/index.php' (custom front controller.)
require('../config.php');
require('../myboot.php');
routeApp($apps);
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration(MY_APP,
MY_ENV, MY_DEBUG);
sfContext::createInstance($configuration)->dispatch();
Please hit me up on twitter if you have any comments/questions. @mikejw