Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Database prefix based on domain
  • Hi guys, I'm working on a Fuel setup where I run multiple sites on one single application. I want to separate the database tables for each site with a database prefix. Based on the domainname requested, the correct prefix should be selected. Now my question is how this can be handled best, and where in the code? To be more specific: - should I use a separate database that contains all the prefix-domain combinations? Or would a flat file (JSON perhaps) be smarter / faster / better? - what's would be the best place to do this domain detection? In the public/index.php? In the app bootstrap? In the database config? Or in a controller after the complete application is loaded? I'm curious to hear some expert opinion on what the best route here is. Thanks, Michiel
  • The easiest and simplest would be to do this in the config/db.php file (or the development/production equivalent). Those are standard PHP files, you can can include any code you want before the array is returned. Determine the prefix there, store it in a variable, and assign that to the prefix parameter in the array. If you want it a bit cleaner, make a separate class for it, and call that from the config file:
    'prefix' => Myclass::get_custom_prefix(),
    
  • Hi WanWizard, Thanks for your reply, that makes sense. While developing more, I figured I actually want to set more variables based on the domainname (e.g. path to assets, version etc.). Now I'm thinking how to implement this is a smart way. Would it make sense to have a siteconfig class, that loads the configuration based on the domain and then store it in a session? I guess this would prevent the system from doing a database query (or file include with some logic to fetch the correct site configuration) on each request. Note that I don't load my assets (css, images, etc.) directly, but load them through an asset class to set the correct expiration headers. Also, how do you estimate the difference between the resources needed to load a file config as opposed to a database query? Thanks!
  • That entirely depends on your environment, and how it is setup. If processing and respons time is your main concern, and you expect high volume for your application, session would be a good idea providing you store those in for examle memcached or redis, or alternatively in the filesystem. I would not go for db sessions in this case.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion