Robert,
I've had this problem, both between production, beta and development
servers but also extended it to having a base "library" from which I can
quickly build additional services, but yet share a common configuration.
I've got a class that encapsulates the configuration (Extra_Config) --
which originated pre-ZF for my environment -- providing two key methods:
setSection(string)
get(key, default)
With that I then have this section in my index.php, which given a
hostname determines the appropriate section of the configuration to use.
index.php
Extra_Config::setSection('hosts');
//
// Figure out based on the HTTP_HOST what config to use...
//
$h = explode('.', strtolower($_SERVER['HTTP_HOST']));
while ($h && count($h) != 0) {
$k = join('.', $h);
if (($section = Extra_Config::get(join('.', $h))) != null) {
break;
}
array_shift($h);
}
if (!$section && (($section = Extra_Config::get('root')) == null)) {
throw new Exception("Unknown configuration");
}
Extra_Config::setSection($section);
Here's an example of the config.ini:
config.ini
[hosts]
foo.example.com = production
beta.example.com = beta
random.example.com = dev
[production]
set a bunch of keys
[ beta : production ]
override a few things for a beta enviornment
Then whenever I need a configuration I just do:
Extra_Config::get('key')
The beta/production/dev split has already been done and avoids moving it
to a per-class basis.
--koblas
robert mena wrote:
Hi,
I've adopted ZF as the framework that I am rewriting my modules. One
of the things I'd like to do is have a better way of changing /
defining some of my modules' behaviour.
I am considering using Zend_Config (not sure if .ini o .xml) and have
development, stagging and production config files. When the bootstrap
is called I find out which enviroment I am using and in my __construct
I load the appropriate config file.
Basically I have 3 questions:
a) should I use Zend_Config or is there any better way?
b) which format should I use? ini or xml? The parameters can be
numbers, strings or mixed arrays
c) If I use Zend_Config I am planning something like the snippet
below. Any advice on using it or a smarter way?
.../blog/application/<models,views,controllers>
.../blog/application/config/devel.xml, stagging.xml production.xml
index.php
...
if(some tests do define the environment as devel)
Zend_Registry::set('ENVIRONMENT','devel')
elseif(some tests do define the environment as stagging)
Zend_Registry::set('ENVIRONMENT','staggingl')
else
Zend_Registry::set('ENVIRONMENT','production')
blog/IndexController.php
class Blog_IndexController
{
function indexAction()
{
... throw an Exception if the environment is not defined...
$blog = new Blog(...,Zend_Registry::get('ENVIRONMENT');
}
}
Blog.php
class Blog
{
function __construct(...,$environment)
{
$config = new
Zend_Config(/path_to_module/config/".$environment.".xml") ;
}
}
Thanks.