0
votes

I have a few view helpers that add JavaScript files when they're needed (for instance, so that only forms use CKEditor and such). My directory structure (simplified to include only the relevant files) is this:

application
    --forms
        --Project
            AddIssue.php
    --modules
        --default
            --views
                --helpers
                    JQueryUI.php
                    Wysiwyg.php
        --project
            --controllers
                ProjectController.php
            --views
                --scripts
                    --project
                        version.phtml
                        issueadd.phtml

What I want to do:

  1. include CKEditor in the view project/project/issueadd
  2. include jQuery UI in project/project/version

When I'm inside the view script, calling <?php $this->jQueryUI(); ?> works like a charm, even though the helper is in the default module's helpers directory. However, the same is not true for the controller and the form.

In the controller ProjectController.php, versionAction(), I tried to call:

$this->view->jQueryUI();

and the effect was an exception:

Message: Plugin by name 'JQueryUI' was not found in the registry; used paths: Project_View_Helper_: C:/xampp/htdocs/bugraid/application/modules/project/views\helpers/ Zend_View_Helper_: Zend/View/Helper/

Similarly, in the AddIssue.php form, I tried this:

$this->getView()->wysiwyg();

and there was an exception again:

Message: Plugin by name 'Wysiwyg' was not found in the registry; used paths: Project_View_Helper_: C:/xampp/htdocs/bugraid/application/modules/project/views\helpers/ Zend_View_Helper_: Zend/View/Helper/

Obviously, both would work if my view helpers were in the helper directories of the modules/controllers they're being called from, but since they're used across many modules, I'd like to have them in the default module's view helpers directory.

So, my questions are:

  1. How do I access those view helpers from within the controller and the form?
  2. Is there a simpler way to get around this (apart from simply including all javascript files in the layout)? Like creating a plugin or an action helper? (I haven't done these things before, so I really don't know, I'm only starting my adventure with ZF).
3
Regarding question 1. You should be able to access the helpers in a usual way. However since it does not work, I think there is a problem with the way you bootstrap your view resource and the way how you perform concert registration of the helper or how you add helper path to it. I paste an example of adding helper path in Bootsrap.php: pastebin.com/5z6yzjnx .This off course should normally work for modular setup of ZF.Marcin
You are using JQueryUI.php. Is it something out of the ZF box or is it your own cooking?akond
@akond: it's my cooking. It simply adds jquery.js, jqueryui.js and ui.css to the page's head section, nothing fancy. I only use it on one page, where I need some draggables and figured including it on all pages made no sense. @Marcin: your insight is invaluable as usual. Would you mind posting it as an answer?mingos
I made an answer from my comment. Happy I could help.Marcin

3 Answers

3
votes

Regarding Q1 (based on the comments). You should be able to access the helpers in a usual way. However since it does not work, I think there is a problem with the way you bootstrap your view resource and/or the way how you perform concrete registration of the helpers or how you add helper path to it. I paste an example of adding helper path in Bootsrap.php:

<?php
#file: APPLICATION_PATH/Bootstrapt.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initViewHelperPath() {

        $this->bootstrap('view');
        $view = $this->getResource('view');

          $view->addHelperPath(
                APPLICATION_PATH . '/modules/default/views/helpers',
                'My_View_Helper' // <- this should be your helper class prefix.
        );
    }

}
?>

This off course should normally work for modular setup of ZF.

Regarding Q2: You can use headScript view helper to manage what scripts do you load in the head tag of your layout. Using this helper you can do it from your actions.

For example. If in a layout.php you have:

<head>
    <?php echo $this->headScript(); ?>
</head>

then in, e.g. indexAction you can append some JS file as follows:

$this->view->headScript()->appendFile($this->view->baseUrl('/js/someJS.js'));
3
votes

As much as I hate answering my own questions, there's one more solution I came up with, based on what Marcin has suggested in his answer. It can also be done in application.ini:

resources.view[] =
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/modules/default/views/helpers"

The caveat is that the lines need to appear in this order. Should it be reversed, anything before resources.view[] = will be ignored.

0
votes

I'd rather get rid of your JQueryUI.php and would use ZendX. Something like that:

In controller:

    ZendX_JQuery::enableView ($this->view);
    $this->view->jQuery ()->enable ()->setRenderMode (ZendX_JQuery::RENDER_ALL);

In layout:

<?php echo $this->jQuery () ?>