1
votes

Where and how to set variable value that is available in all controllers. I don't wont to use zend registry and don't want to extend Zend_Controller_Action. Is there is another way? I just want for example to set:

$a = "test";

and in Index controller to dump it:

class IndexController extends Zend_Controller_Action {

    public function indexAction(){

            var_dump($a);
        }
}
2
Why do you not want to use the Zend_Registry?Richard Parnaby-King

2 Answers

1
votes

Global vars ruin the purpose of object oriented programming... use namespace or custom configs.

Solution 1

Use session Zend_Session_Namespace, here is documentation on how to Zend_Session_Namespace.

  1. Set set the value in namespace in bootstrap or something (wherever you see fit)
  2. Retrieve the value from namespace in you controller/model/other

Solution 2

Alternatively, you can create some new class with static properties and use it's setters/getters to set and retrieve values.

E.g.

class SomeClass
{    
    static $hello = 'world';
}

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        var_dump(SomeClass::$hello);
    }
}
0
votes

You can add variables to the request object:

$this->getRequest()->setParam('a', 'hello');

Then retrieve it using:

$this->getRequest()->getParam('a);

But that is not the best way of doing it as you might accidentally overwrite a parameter a needed parameter.