Zend: Where/how can I register custom view helpers

2019-02-01 14:15发布

In my layout.phtml file I have :

<?php echo $this->Test(); ?>

I have created a Test view helper at application/views/helpers/Test.php

<?php 

class My_View_Helper_Test extends Zend_View_Helper_Abstract {

    public function Test() {
        return 'test';
    }

}

And my config file @ configs/application.ini:

resources.view[] = ''
resources.view.helperPath = APPLICATION_PATH "/views/helpers"

Error I get:

Zend_Loader_PluginLoader_Exception: Plugin by name 'Test' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/:./views/helpers/ in /usr/share/php/Zend/Loader/PluginLoader.php on line 406

On a similar note I can't register my admin view helper either..

resources.view.helperPath.Admin_View_Helper = APPLICATION_PATH "/modules/admin/views/helpers"

My modules/admin/views/helpers/AdminPanel.php:

<?php

class My_View_Helper_AdminPanel extends Zend_View_Helper_Abstract {

public function AdminPanel() { return 'test'; }

}

Do I have no choice but to do this in the Bootstrap with addHelperPath? If so could someone demonstrate how I would using my paths?

3条回答
SAY GOODBYE
2楼-- · 2019-02-01 14:27

Using application.ini is probably the best way to define these. I put all my view helpers inside my library folder:

includePaths.library = APPLICATION_PATH "/../library"
autoloadernamespaces.0 = "SNTrack_"

;  -- Note, these are the only resources.view lines I have...
resources.view.doctype = "XHTML1_STRICT"
resources.view.helperPath.SNTrack_View_Helper = APPLICATION_PATH "/../library/SNTrack/View/Helper"

Directory structure:

/
  application/
  library/
    SNTrack/
      View/
        Helper/
          Test.php

View:

 $this->test('test')

SNTrack/View/Helper/Test.php:

 class SNTrack_View_Helper_Test extends Zend_View_Helper_Abstract {
   public function test($args) { return $args; }
 }
查看更多
三岁会撩人
3楼-- · 2019-02-01 14:34

in my bootstrap:

$view = new Zend_View();
$view->addHelperPath(DE_Config::get('DE_appDir').DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'DE'.DIRECTORY_SEPARATOR.'View'.DIRECTORY_SEPARATOR.'Helper'.DIRECTORY_SEPARATOR, 'DE_View_Helper');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
查看更多
仙女界的扛把子
4楼-- · 2019-02-01 14:51

I just had this exact problem, and realised it was due to a problem in my bootstrap: I was defining and using a new Zend_View object in one of my _init functions, which I think was overwriting all my other view settings from both my bootstrap and my application.ini file (including my resources.view.helperPath definition). The offending code had been blindly copied from here, and placed into an _initJQuery() function in my bootstrap, which looked like this:

protected function _initJQuery() {
   $view = new Zend_View();
   $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');

   $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
   $viewRenderer->setView($view);
   Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}

The solution was to replace the first line ($view = new Zend_View()) with this:

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

Another thing to bare in mind, regarding your line:

resources.view.helperPath = APPLICATION_PATH "/views/helpers"

Note that this only registers the path, and not the class prefix, so this will only work if the helper classes have the default Zend class prefix of Zend_View_Helper i.e. Zend_View_Helper_Test. If you want the class to be My_View_Helper_Test, then you need to do this:

resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/views/helpers"

查看更多
登录 后发表回答