2

I am designing a site with following url form:
example.com/controller/action/locale
In which I get the locale from uri parameter locale.

In bootstrap when I want to initialize my resources like Zend_Locale, Zend_Translator or some other resource that configure Zend_Validate_Date like components, they all need locale data. And moreover I could have wanted to use different databases according to recieved locale. But since no uri parameters are normally available in bootstrap I need to initialize all above in the controller plugins, which seems senseless to me.

In conclusion i think the request object and so uri parameters should have been available in bootstrap. So the current design of Zend Framework is missing this point.

Am I right or missing something?

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

2

Sorry, but what you are suggesting is a bad idea. Bootstrapping is meant to get the library into a working state by initializing required settings, variables, etc.

Some things bootstrappers should do:

  • Add custom paths to 'include_path'
  • Initialize character sets (UTF-8) and encoding directives (mb_internal_encoding)
  • Initialize loggers (error or app logging)
  • Initialize autoloaders

Your application should be handling your requirements at the controller layer. For example if a user visits example.com/controller/action/en-US, your controller can set the language accordingly by accessing the request object (and specified parameter) and set a user session var to display the current and subsequent pages in english.

-- Edit --

Example implementation for initializing i18n/locale settings using an intermediary class vs. passing values to bootstrap:

// Controller

$i18n = new i18n();

$i18n->setLocale($this->getRequest()->getParameter('locale'));

// Now I can make locale specific calls to validate localized data
$i18n->validateDate($this->getRequest()->getParameter('date'));

// Can also make queries for locale specific data
$results = $i18n->getDob()->query('select * from my_table');

// i18n class
class i18n
{
    protected $locale;

    public function setLocale($locale)
    {
        $this->locale = $locale;
    }

    public function getLocale()
    {
        return $this->locale;
    }

    // Factory method for creating a database object based on locale
    public function getDbo()
    {
        switch ($this->getLocale()) {

            case 'en-US':
                return new Zend_Db::factory('Pdo_Mysql', array(
                    'host' => 'hostname',
                    'username' => 'username',
                    'password' => 'password',
                    'dbname' => 'en_us_locale'
                ));

            case 'en-GB':
                return new Zend_Db::factory('Pdo_Mysql', array(
                    'host' => 'hostname',
                    'username' => 'username',
                    'password' => 'password',
                    'dbname' => 'en_gb_locale'
                ));
        }
    }
}
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • ok but what if some configurations of my resources like include paths, database etc.. does change according to the locale parameter. Than I can not initialize them in the bootstrap. Should I initialize them all in controller plugin? I dont think this is a good idea? – Mucahit Sancar Kahveci Jan 21 '12 at 09:13
  • You could create a higher level bootstrap class. Pass the request object to it, then initialize your include_paths and database connections based on current value. End game here is to not pass request level data to your base bootstrap as it should be as dumb as possible and only initialize the library for usage. – Mike Purcell Jan 21 '12 at 21:14
  • Yes maybe this can be a good solution. Creating a controller plugin as bootstrap and altering or initializing some bootstrap resources here. – Mucahit Sancar Kahveci Jan 22 '12 at 14:22
  • But the main thing is that ZF team should improve the framework to offer someway to get uri params within bootstrap. – Mucahit Sancar Kahveci Jan 22 '12 at 14:29
  • You could set up a generic i18n class to set-up your database connections, and locale settings by passing uri params to it by way of public setters. I updated post with some sample code, see if helps. – Mike Purcell Jan 22 '12 at 21:48
  • @MucahitSancarKahveci: Any luck? – Mike Purcell Jan 26 '12 at 00:59
  • Well the problem can be solved using different ways. Bu what i wonder is if a framework should be providing uri params at bootstrap or not. So maybe we can notify ZF developers to make params available in ZF 2.0 – Mucahit Sancar Kahveci Jan 26 '12 at 20:47
1

What you need to do is write a custom plugin I think.

I would start with looking at this link to get familiar with plugins: http://framework.zend.com/manual/en/zend.controller.plugins.html

Then take a peek at this (it seems to be part of what you want): http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.locale

and lastly if you are creating your own plugin the key is in using the

$request->getParam();
variable in the (probably for what you are doing) preDispatch function

The following is a little guy that switches my layout file based on the module of the request - hopefully it gives you some insight.

<?php
    /*
 *  Theme Switcher 
 *  Set the current module name as $this->skin in the layout
 *  
 *  
 */
class My_Controller_Plugin_Themer extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module_name    = $request->getModuleName();
        $view       = Zend_Layout::getMvcInstance()->getView();
        $view->skin     = $module_name;
    }
}?>
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
  • Currently I am using some plugin like this for locale, that makes locale resource set config options in bootstrap but actually initialize in controller plugin. But then I need to redesign every resource using locale data to lazy load until they actually need locale data. So I extended Zend_Locale, Zend_Translator to lazy load actual initialization, which i think is the reason of poor design of Zend Framework – Mucahit Sancar Kahveci Jan 21 '12 at 09:24