Resolve view helper location from within the controller or form

时光怂恿深爱的人放手 提交于 2019-12-08 09:53:10

问题


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).

回答1:


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'));



回答2:


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.




回答3:


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 () ?>


来源:https://stackoverflow.com/questions/4978389/resolve-view-helper-location-from-within-the-controller-or-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!