4
votes

I've read somewhere that using a base controller is bad and that there are more down sides than up sides. That person said that you should use plugins.

What I need is before every action get from the request the "lang" variable and pass it to the current action. What I've done now is making a base controller with preDispatch that gets it from the request and passes it through $this (any other controller extends from the base).

How should I implement it if I'd use plugins? And should I?

EDIT: Found the place where I've read that base controllers are evil: Sending variables to the layout in Zend Framework comment on the last answer. Notice that my question is not similar (I need to pass to an action, not to the layout).

EDIT2: With your answers on how to implement, could you also include an explanation why using a base controller is bad?

EDIT3: Can't seem to make it work. I've done: created a helpers dir in the controllers folder, added in the initializer Zend_Controller_Action_HelperBroker::addPath('../application/default/controllers/helpers/', 'Controller_Helper'); Created a file in that folder called LangHelper.php and created a class Controller_Helper_Lang extends Zend_Controller_Action_Helper_Abstract. Why doesn't it yet work? (maybe I need to add a require once or something?)

EDIT4: What I get is:

Zend_Loader_PluginLoader_Exception: Plugin by name 'Lang' was not found in the registry; used paths: Controller_Helper_: ../application/default/controllers/helpers/;../application/admin/controllers/helpers/ Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/ in C:\wamp\www\EfCom\library\Zend\Loader\PluginLoader.php on line 412

1

1 Answers

2
votes

You should rather use Action Helpers, not plugins.

That way, you could do for example $this->_helper->getLang() to get the lang in your action (with GetLang being your Action Helper), rather than use a class attribute.

Plugins are useful to control the routing of the request (for example to add ACL filtering). This is not what you want to do here.

Example code for your helper:

class MyModule_Controller_Helper_GetLang extends Zend_Controller_Action_Helper_Abstract {
    /**
     * direct() is the default method called
     * when you use $this->_helper->getLang()
     */
    public function direct() {
        $lang = /*get you lang here*/;
        return $lang;
    }
}

Tutorials:

I recommend to put your helpers in /application/controllers/helpers. See the official recommendation for directory layout. They say :

controllers/helpers/ - These directories will contain action helpers. Action helpers will be namespaced either as "Controller_Helper_" for the default module or "Controller_Helper" in other modules.

Update:
I've used a base controller before knowing about action helpers, that does the job, but lets say helpers are there exactly for that. This is a concept created exactly for what you want to do, with some advantages (like lazy loading, so that the helper is loaded only when you use it). Imagine in a week you need to add another variable, but needed only in some pages, not all. With a base controller, those variables would be loaded every time. You should rather learn how to do it right (with helpers), so that you can use it fully later. That will keep you code clean and organized. A controller is just a controller, a helper is just a helper.