Zend Framework - Getting request object in bootstrap

2.2k views Asked by At

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?

2

There are 2 answers

7
Mike Purcell On

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'
                ));
        }
    }
}
1
Francis Yaconiello On

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;
    }
}?>