views:

985

answers:

3

Hello,

ATM I set the currency and locale objects in the bootstrap and assign them to zend registry.

Then in the controller that needs them, In the init I do:

    public function Init() {
    $this->locale = Zend_Registry::get('locale');
    $this->currency = Zend_Registry::get('currency');
}

I was wondering if there's away to make variables and objects accessible through the controller classes?

I figure the controllers extend Zend_Controller_Action so I just need to make it a var in that class somehow in the bootstrap?

Edit, so after much testing, I've added this to my bootstrap:

    protected function _initFrontTController() {

    $frontController = Zend_Controller_Front::getInstance();
    //$frontController->setControllerDirectory(ROOT_PATH.'/application/modules/', 'default');

    if($frontController->getParam('bootstrap') === null) {
        $frontController->setParam('bootstrap', $this); 
    }

    return $frontController;
}

If I name it _initFrontController I get

Zend_Controller_Exception: No default module defined for this application in

If I set it to the right module, I get:

Zend_Controller_Dispatcher_Exception: Invalid controller specified (error) in

Not sure the best way to proceed, for now it works I guess :(

A: 

Using $this->getInvokeArg('bootstrap') from within your controllers will give you a handle to your bootstrap object. You can then call getResource() on your bootstrap class to get something that was initialized.

If you have the following functions in your bootstrap:

protected function _initLocale()
{
     // Do whatever here
     return $locale;
}

protected function _initCurrency()
{
     // Do whatever here
     return $currency;
}

You can then fetch them from your controller:

public function init()
{
    $bootstrap = $this->getInvokeArg('bootstrap');
    $this->locale = $bootstrap->getResource('locale');
    $this->currency = $bootstrap->getResource('currency');
}

Edit:

From the pastebin

You're overriding the run() function and never calling the parent function. That's why it's never setting the invoke param for the controller, as you are handling the dispatching of the frontController yourself. Nothing wrong with that but you'll have to set the invoke arg yourself. Set the bootstrap param on the frontController:

$frontController->setParam('bootstrap', $this);
smack0007
Would you suggest putting all the stuff I have in run into a new function thus keeping the parent run method going as is.
azz0r
I think really what you do in the run() function can be done in seperate _init*() methods.
smack0007
Ah thanks, due to time constraints I put:$this->frontController->setParam('bootstrap', $this);in my run function and it works fine!
azz0r
A: 

In reply to the above, my code is now:

bootstrap:

    protected function _initLocale() {
    $this->bootstrap('layout');
    $layout     = $this->getResource('layout');
    $view       = $layout->getView();
    $translate  = new Zend_Translate('gettext', APPLICATION_PATH.'/languages/', NULL, array('scan' => Zend_Translate::LOCALE_FILENAME));
    $session    = new Zend_Session_Namespace('language');
    $locale     = new Zend_Locale();

    #Set Locale 
    if (isset($session->language)) {
        $requestedLanguage = $session->language;#user set locale manually
        $locale->setLocale($requestedLanguage);
    } else {
        $requestedLanguage = $locale;#user set no session so default to browser
        $locale->setLocale($requestedLanguage);
    }

    #Check we have a language file for it
    if (in_array($requestedLanguage, $translate->getList())) {
        $language = $requestedLanguage;#if requested language is in our list, use it
    } else {
        $language = 'en_GB';#otherwise default to english
    }

    //Zend_Registry::set('locale', $locale);
    $translate->setLocale($language);
    $this->view->translate = $translate;
    return $locale;
}


protected function _initCurrency() {
    $this->bootstrap('locale');
    $locale     = $this->getResource('locale');
    $currency   = new Zend_Currency($locale);
    return $currency;
}

my controller is then:

<?php


class Video_IndexController extends Zend_Controller_Action {


public function Init() {
    $bootstrap = $this->getInvokeArg('bootstrap');
    $this->currency = $bootstrap->getResource('currency');
    $this->locale   = $bootstrap->getResource('locale');
}

but I get:

Fatal error: Call to a member function getResource() on a non-object in /Users/aaron/Sites/Eurocreme/eurocreme.com/application/modules/video/controllers/IndexController.php on line 9
azz0r
$bootstrap = Zend_Controller_Front::getInstance()- >getParam('bootstrap'); Also does not work
azz0r
Please respond in the future by editing your question.
smack0007
1.9.6.Ah okay will do in future.
azz0r
A: 

Perhaps you could have all your controllers extend a base controller that queries the registry and sets the currency and locale variables.

Chris