views:

48

answers:

2

I try to follow this tutorial, but I can't get it to work:

http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html

I did everything as described, but I don't know how to make it available in my controllers. My filesystem looks like this:

- application
    - controllers
        - IndexController.php
    - modules
        - user
            - configs
                user.ini
            - controllers
            - forms
                Login.php
            - helpers
                HandleLogin.php
            - views
                - scripts
                    login.phmtl
                    profile.phtml
            Bootstrap.php
    - views

How do I use the HandleLogin Helper in my IndexController? I really have no idea and I'm looking an trying for more then a day and I almost want to throw my PC out of the window ;). So any help would be appreciated!

A: 

You dont. The point of the tutorial is to create a reusable widget that runs independent from any specific controllers. When the application receives a request, it will run through it's dispatch cycle and trigger the action helper on preDispatch automatically:

Now, let's look at the action helper itself. As a reminder, action helpers can define hooks for init() (invoked by the helper broker each time it is passed to a new controller), preDispatch() (invoked prior to executing the controller's preDispatch() hook and executing the action itself), and postDispatch() (executed after the action and the controller's postDispatch() routine).

The helper will fetch the current controller (whichever that may be for that request) to get the View instance and configure it with the form

Gordon
The problem is, nothing happens when I try to load an action in my IndexController. Even if I put die statements in, for example, helpers/HandleLogin.php, nothing happens. So the code is just not executed. Any ideas?
Stegeman
By *nothing happens* you mean *nothing at all* or no plugin? Can you validate the module is actually [recognized and loaded in your main bootstrap](http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.modules "ZF Reference Guide on Zend_Application modules")?
Gordon
The page loads as it should load, only the helper is not executed. If I add an controller to the module and try to access it, it works. So I assume that means the module is loaded correctly. By the way, is there a better way to check this?
Stegeman
Hmm, odd. The best way would be to use a Debugger and step through the code to see what happens.
Gordon
+1  A: 

Looks like the widget plugin is not called anywhere.

Few things to check:

  1. Do you have a Bootstrap.php file for the module?
  2. Does this bootstrap file has _initWidgets() method?
  3. Does this method call:

    $widget = new Module_Widget_Name; // is it callable?
    Zend_Controller_Action_HelperBroker::addHelper($widget);

  4. Have you registered widget resource?

    public function _initResourceLoader()
    {
    $loader = $this->getResourceLoader();
    $loader->addResourceType('helper', 'helpers', 'Helper');
    $loader->addResourceType('widget', 'widgets', 'Widget');

    return $loader;
    

    }

  5. Does application.ini contains resources.modules[] = line?

takeshin
Yes, there is a bootstrap file (see my original post). I added the methods you mentioned. I suppose the _initResourceLoader() should also be in the modules bootstrap file? But I think the main problem is that the complete bootstrap file of the module is not executed so adding methods to it seems, at least to me, useless. Or am I missing something?
Stegeman
@Stegeman I added one more point to check. If you don't use modules at all, you may use main Bootstrap file as well.
takeshin
I don't know which one solved my problem, but I managed to fix it with one of the checks above. Thanks!
Stegeman