How to set up site specific configuration vs application configuration in Zend Framework?
Being fairly new to Zend Framework, I've been reading and trying out various tutorials on the web and books I've purchased. One thing all the tutorials do is hard code certain values into into the bootstrap or other code. For example, setting the title:
$this->_view->headTitle('MySite');
I realize this can be set in the application.ini file, but I don't think that is appropriate either if you are distributing the application to other sites.
I would be interested in hearing ideas where application specific settings are set in the application.ini file and loaded:
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH.'/configs/application.ini'
);
Then somewhere in the bootstrap, checking for a config.ini file and adding these to currently existing application config array, and if config.ini does not exist, retrieving such site specific configs from a database and writing the config.ini file (Obviously the file deleted and rewritten if a value is changed in the database). I don't need to see how the file is written or what not... just a general idea of how others are handling such things. Or provide different ideas of doing this?
开发者_运维知识库I would rather end up using something like this when setting various site specific configurations:
$this->_view->headTitle($config->site->title);
Hope this makes sense :-)
It depends a bit what kind of data you want in your config files, and how you are reusing your application on different sites.
Although it's normal to pass the filename to your config file as the second parameter to Zend_Application, you can also pass a Zend_Config object. Zend_Config itself makes it very easy to merge together multiple config files, so in your public/index.php you could do something like this:
$defaultConfigFilename = APPLICATION_PATH . '/configs/application.ini';
$siteConfigFilename = APPLICATION_PATH . '/configs/site.ini';
// Create config object, using site-specific data if available
$config = new Zend_Config_Ini($defaultConfigFilename, null, true);
if (file_exists($siteConfigFilename)) {
$siteConfig = new Zend_Config_Ini($siteConfigFilename);
$config->merge($siteConfig)
->setReadOnly();
}
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
$config
);
this will look for a site.ini file, and if it exists, merge it with the application ini. Then it will bootstrap the application as normal.
Another way that I've used myself recently is to just keep application.ini as minimal as possible (e.g. only bootstrap class name and location), and then have an _initSite() method in my bootstrap class that creates a Site object using data from the database. It then reads in config data from a ini file, and stores this in the Site object. I then have a site resource that I can access elsewhere in my application, for example to do $view->_headTitle($site->config->title); like in your example above.
I hope this gives you some ideas. ZF is pretty flexible!
Remember, that you may pass many config files to Zend_Application, not just application.ini, so this might be the best in your case.
If you heavily rely on configs, you may be interested on creating additional application resources, using specific setting you provide to the Zend_Application via config.ini.
But I bet, it the future, you will store these options in the database and allow end user to modify them.
In the simplest case, solution I prefer:
// in the layout.phtml
$this->render('head.phtml');
...
$this->render('footer.phtml');
And in the footer and head configuration specific to the site.
Fast and easy to maintain.
in view_script.phtml
$this->headTitle()->prepend($config->site_title);
where the trouble is?
精彩评论