After reading the Symfony book it was not clear to me how to deal with servers in the config setting. Say that you are creating a site on your local computer and then want to deploy it to your server. Some variables might be different, like in my case the image directory.
Symfony nicely support setting variables for development environments, but I found no such functionality for different servers. In the meanwhile I’ve created a simple filter setup which deals with the problem very nicely.
First set your config values in the frontend/config/app.yml:
all:
server: 'local'
local:
imagedir: 'http://comments/comment/images/'
production:
imagedir: 'http://www.mellowmorning.com/symfony-examples/images/'
Next create a filter in lib/filters/setconfFilter.class.php:
class setconfFilter extends sfFilter
{
public function execute($filterChain)
{
// Execute this filter only once
if ($this->isFirstCall())
{
define('SERVER', sfConfig::get('app_server'));
define('IMAGE_DIR', sfConfig::get('app_'.SERVER.'_imagedir'));
}
// Execute next filter
$filterChain->execute();
}
}
Now to tie our little labor together set in frontend/config/filters.yml between security and cache:
setconf: ~
class: setconfFilter
Here you now have your own server dependant config settings, just use IMAGE_DIR in your template and it will refer to the correct location. If you are really lazy and do not want to change your app.yml every time you sync with production, we could automate that in the filter as well. Simple write something like this in the filter to define the SERVER constant:
define('SERVER', ($_SERVER['SERVER_NAME']=='comments')?'local':'production');
and remove the definition of the server from the app.yml.
Read more: About filters
Read more: the Symfony configuration system
PS. Also have a look at some of the great cheatsheets out there, from the Symfony wiki:
Admin generator cheat sheet
Directory structure and CLI
View
View: Partials, Components, Slots and Component Slots
Form Helpers
Javascript and Ajax Helpers
Server Validation
Model
Model: Schema
Lime Unit & Functional Testing
ORM Diagram
rpsblog.com » A week of symfony #38 (17->23 September 2007) responded on 23 Sep 2007 at 9:38 pm #
[...] Quick Symfony Trick – The power of filters [...]
Jo responded on 24 Sep 2007 at 2:45 pm #
this is what I was looking for.
Thanks
jo
hardrock responded on 24 Sep 2007 at 6:26 pm #
Why not use two different environments? I’m using the environments ‘live’ and ‘prod’ here, sharing all except connection specific settings.
Regards
tschellenbach responded on 24 Sep 2007 at 7:24 pm #
In most cases that is also a viable solution, however that approach
- forces you to have a duplicate settings specification for two environments ( I am unsure how much of an issue this becomes with larger configs )
- requires more duplication if you would like to have access to multiple environments on your live server.
For me the above approach become more attractive because i also wanted to define constants in a filter, so the effort was about the same.
vbali blogja » links for 2007-09-29 responded on 29 Sep 2007 at 10:21 pm #
[...] Quick Symfony Trick – The power of filters Eltér? konfigurációs beállítások alkalmazása különböz? szerverekhez symfony flterrel. (tags: symfony framework development php webdev) Oszd meg ezt a bejegyzést : Ezek az ikonok ún. közösségi könyvjelz? oldalakra mutatnak, ahol meg lehet osztani másokkal is amit fontosnak találsz. [...]
Lukas responded on 04 Oct 2007 at 8:53 am #
Yeah, different env’s are not what you want. Env’s are for separating if I want a debug toolbar to show up or not. But for preproduction testing (or deploying on multiple servers in a cluster), its not feasible to have a dev and prod config for all permutations.
The solution I found is the following:
- On all but the most default dev setup, I need to place a file with a unique identifier for the server (or application setup) in a file in the root dir of the symfony app
- This file is read inside the index.php (frontend_dev.php etc) and is put into a constant
- Now inside my yml files I simply add some PHP code that checks this constant and echo’s the proper setting out in the yml file
This approach as the advantage of leverging the yml file cache instead of adding another filter, which is a significant overhead. In theory I could also cache the contents of the file that identifies the application setup. I guess I could also put something into the .htaccess file or the vhost setting
Simon responded on 12 Oct 2007 at 9:57 am #
you could also define some settings in the settings file, and use symfony’s already defined production / test / development settings, by defining which index it hits..
Thats similar, but doesn’t require you to create another filter, which has to be pre-processed..
My Hobby is Programming » Blog Archive » Quick Symfony Trick - The power of filters responded on 30 Oct 2007 at 11:24 am #
[...] read more | digg story [...]
tomasz ducin responded on 15 May 2010 at 11:29 am #
You can see how to use advanced admin filters in new symfony 1.3-1.4 releases at http://symfony-world.blogspot.com/2010/01/custom-admin-generator-filter-example.html