zend-frameworkzend-view

how I add view helper in zend


I want to add custom view helper in zend framework like this:

  1. I placed in application.ini this code:

    includePaths.library = APPLICATION_PATH "/../library"
    and create library directory in myproject root

  2. create view helper TabEntry.php in library directory

    class Zend_View_Helper_TabEntry extends Zend_View_Helper_Abstract {

    public function TabEntry() {

    }
    }

  3. create another view helper TabEntries.php in library directory

    class Zend_View_Helper_TabEntries extends Zend_View_Helper_TabEntry {

    public function TabEntries() {

    }
    }

  4. when in my phtml use $this->TabEntries() get error
  5. in Bootstrap.php I add some code:
    $view->addHelperPath('MyView/Helpers', "library_MyView_Helpers");
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); $viewRenderer->setView($view);

Solution

  • According to ZF coding application structure, correct version would be:

    In application.ini:

    resources.view.helperPath.Your_View_Helper = "Your/View/Helper"
    

    Then the helpers: (not sure why do you need another abstract class):

    // library/Your/View/Helper/TabEntry/Abstract.php
    
    class Your_View_Helper_TabEntry_Abstract extends Zend_View_Helper_Abstract {
        public function tabEntry($param1, $param2) {} // note the lower case here
    }
    
    // library/Your/View/Helper/TabEntries.php
    
    class Your_View_Helper_TabEntries extends Your_View_Helper_TabEntry_Abstract {
        public function tabEntries($param1, $param2) {...} // note the lower case
    }
    

    In the view:

    $this->tabEntries();
    

    Important: call_user_func and Linux filesystem are case sensitive.