Assign Symfony environments to (sub)domains
With Symfony, for example, you can just point your staging domain or subdomain to the exact same location and set the environment on the fly.
This way you are using the exact same PHP files, but you have control over the usual environment settings such as database connection, application variables, email recipients etc etc.
In Symfony 1.2 you just modify your index.php controller like so:
<?php
##IP_CHECK##
require_once(dirname(__FILE__).'/../../config/ProjectConfiguration.class.php');
//check for DEMO site
$env = "live";
$debug = false;
$domain = $_SERVER['HTTP_HOST'];
if (strpos($domain,"demo") !== false){
$env = "demo";
$debug = true;
}
$configuration = ProjectConfiguration::getApplicationConfiguration('project_name', $env, $debug);
sfContext::createInstance($configuration)->dispatch();
Now just set your database in databases.yml
demo:
propel:
class: sfPropelDatabase
param:
dsn: mysql:dbname=demo_database;host=demo_server
username: demo_user
password: demo_pass
encoding: utf8
persistent: true
pooling: true
classname: DebugPDO
And finally, set your application settings in your app.yml
demo:
contact_email: contact@demo.com
#etc
If you want to make your staging/demo site appear differently, you can add a filter that inserts a stylesheet:
class demoFilter extends sfFilter
{
public function execute ($filterChain)
{
// execute this filter only once
$env = sfContext::getInstance()->getConfiguration()->getEnvironment();
if ($env = "demo" && $this->isFirstCall())
{
$this->getContext()->getResponse()->addStylesheet('demo','last');
}
// execute next filter
$filterChain->execute();
}
}
Note that the stylesheet should be set to 'last' to ensure you override your default styles.
You also need to add your filter to your filter.yml
rendering: ~
security: ~
demo:
class: demoFilter
#other filters go here
functional_test:
class: swFilterFunctionalTest
cache: ~
common: ~
execution: ~
The handy thing about using a filter is that you can apply it to multiple applications in the same project. Now you can set a background image in demo.css
body {
background-image: url(/images/demo.gif);
}
One Response to "Assign Symfony environments to (sub)domains"
1ServerGrove Blog » Blog Archive » Setting up subdomain virtual hosts for multiple applications in a symfony project February 8th, 2010 23:08
[...] are some hacks (here, here and here) on the net that allows you to define the application and environment based on the domain. These [...]
Share your thoughts