updates: identi.ca, twitter

="sydphp"

Sydney PHP Group provides a community for PHP developers in Sydney, Australia.
Membership is free and open to anyone with an interest in web development.

Now aggregating!

Do you provide web development related services in Sydney and want that information available to the Sydney PHP development community? You can reach our community by getting your RSS/ATOM feed syndicated on sydphp.org

How do I join?

Just attend a meeting, present a topic if you wish or add your thoughts to our blog

Search

Mailing List

The group maintains an announcement mailing list at Google Groups, used to notify members of upcoming web development events in the Sydney region

Events Calendar

Subscribe to the sydphp calendar and receive event notifications as they happen

Flickr pool

Assign Symfony environments to (sub)domains

Some projects need a staging site - somewhere where you can test and play around without messing up your database or webstats. Traditional applications require you to make a complete copy of your site to do this - and that means you need to sync your dev files with 2 remote locations.

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