One backend, many sites

It’s good regularly to read through symfony forum. Sometimes there can be found pearls. It makes me feel I’m ready to keep using symfony if such problems are already solved 🙂

So here is the one:

I have a homegrown CMS that I use for my clients that I want to port over to the Symfony framework eventually. I have a fair amount of experience using Symfony, but I’m wondering if it can accommodate what I’m looking for.

Right now, I have one instance of a backend that all my clients log into. I do this so that when I make changes to the backend, I only have to roll it out once, instead of making updates to dozens of sites. When a client logs into the CMS, it fetches his/her associated “site_id” and manages only pages associated with that site by filtering every query. Likewise, on the frontend, a local config file tells the controllers to only get records in the CMS that are associated with that site_id…

It seems like Symfony and most other MVC frameworks would rather I have one backend and one frontend, but in this case I have one backend and many frontends… Although they all share the same modules (blog, imagegallery, etc..), so it’s really only one frontend with just the templates and local files changing from site to site.

One thing I considered was setting up a backend application for every site and giving it a symbolic link to the shared backend, but that doesn’t seem like the best solution. Any ideas?

And here is Guru’s response:

I think your problem is easy to solve. You just need to configure connection for each of the databases in your config/databases.yml (unfortently we still need to set default ‘propel’ connection):

class: sfPropelDatabase
dsn: pgsql://
class: sfPropelDatabase
dsn: pgsql://
class: sfPropelDatabase
dsn: pgsql://
class: sfPropelDatabase
dsn: pgsql://@localhost/default-db

While logging user in you could define his database connection name and store it in the session (depending where you’ll do it code could look different):

sfContext::getInstance()->getUser()->setAttribute('connection', 'propel-client2');

You will then get proper connection and save your object with something like:

$conName = sfContext::getInstance()->getUser()->getAttribute('connection');
$con = Propel::getConnection($conName);
$article = new Article();
$article->setTitle("Test article");
$article->setBody("Blah blah blah");

You could also override your Article’s save() method to set connection automagically.

As for me I’d really preffer to keep everything in single shared db (it is implemented e.g. in WPmu) instead of keep cloning databases. In this case backend manage would be easier to implement. But one think and the other happens. So good to know exists solution like this.

Btw, forum posting url is

One reply on “One backend, many sites”

Leave a Reply

Your email address will not be published. Required fields are marked *