4

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

Community
  • 1
  • 1
Vadiklk
  • 3,696
  • 5
  • 28
  • 44

1 Answers1

2

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.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Could you write how to use this in my case? – Vadiklk Jul 01 '11 at 08:12
  • What do you thing about the base controller idea? – Vadiklk Jul 01 '11 at 08:18
  • 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. 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. – Matthieu Napoli Jul 01 '11 at 08:23
  • Last 2 questions: Can I use $this->getRequest in an Zend_Controller_Action_Helper_Abstract? And where do I initialize/put class/put file/and call it? (Initializer? Bootstrap?) – Vadiklk Jul 01 '11 at 08:24
  • Mmmh there are a lot of tutorials about that I believe. I know for sure that is possible to get the request (this *is* mainly what helpers are for). I added links and infos to my answer. – Matthieu Napoli Jul 01 '11 at 08:28
  • 1
    You can do `$this->getRequest()` in your action helper to access the request object. So `$this->getRequest()->getParam('lang');` will give you your language param. – Tim Fountain Jul 01 '11 at 08:40
  • 1
    @Vadiklk You named you class "Lang" and you file "LangHelper", the autoloader can't find it. I suggest you name them both "Lang" or "GetLang". – Matthieu Napoli Jul 01 '11 at 09:49
  • Thanks! I would have voted up you again if I could of. You helped me alot! – Vadiklk Jul 01 '11 at 10:01