Search code examples
zend-frameworkzend-view

Zend: Custom View helpers


I'm trying to add a View helper in my project, but I'm getting the following error:

[Mon Apr 29 14:36:19 2013] [error] [client 10.0.0.26] PHP Fatal error:  Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'LoggedInAs' was not found in the registry; used paths:\nMy_View_Helper_: /var/www/html/test-project/application/views/helpers/\nZend_View_Helper_: Zend/View/Helper/:/var/www/html/test-project/application/views/helpers/' in /usr/share/php/Zend/Loader/PluginLoader.php:412\nStack trace:\n#0 /usr/share/php/Zend/View/Abstract.php(1182): Zend_Loader_PluginLoader->load('LoggedInAs')\n#1 /usr/share/php/Zend/View/Abstract.php(618): Zend_View_Abstract->_getPlugin('helper', 'loggedInAs')\n#2 /usr/share/php/Zend/View/Abstract.php(344): Zend_View_Abstract->getHelper('loggedInAs')\n#3 /var/www/html/test-project/application/layouts/scripts/layout.phtml(16): Zend_View_Abstract->__call('loggedInAs', Array)\n#4 /var/www/html/test-project/application/layouts/scripts/layout.phtml(16): Zend_View->loggedInAs()\n#5 /usr/share/php/Zend/View.php(108): include('/var/www/html/t...')\n#6 /usr/share/php/Zend/View/Abstract.php(888): Zend_View->_run('/var/www/html/ in / /usr/share/php/Zend/Controller/Plugin/Broker.php on line 336

application/views/helpers/LoggedInAs.php

class My_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract
{
    public function loggedInAs()
    {
        //code
    }
}

application/configs/application.ini

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

application/layouts/scripts/layout.phtml

echo $this->loggedInAs();

There are several other questions on stackoverflow, but these didn't work for me.

edit 1: changed Zend_View_Helper_LoggedInAs to My_View_Helper_LoggedInAs after Tim Fountain's answer edit 2: full error


Solution

  • The answer you provided doesn't seem to make much sense, if you are saying you added <?php to the top of the file where your class is written, all I can say is "Welcome to PHP!" otherwise the following may help in the future... or not.

    Using the master application.ini from feibeck as a reference, I came up with:

    //excerpt from application.ini
    resources.view.helperPath = APPLICATION_PATH "/views/helpers"
    resources.view.helperPathPrefix = "My_View_Helper"
    

    however you are using the defaults for the MVC so you shouldn't require any config at all.

    This may be a case of too much config.

    That being said: I've never been a fan of setting view options in the application.ini as I'm never quite sure of what effect I should expect (am I adding or setting an option?). I much prefer to setup the view in the bootstrap as most of the methods used are more verbose and tell a more complete story:

    //bootstrap.php
    protected function _initView()
        {
            //Initialize view
            $view = new Zend_View();
            //add custom view helper path
            $view->addHelperPath(APPLICATION_PATH . '/../library/My/View/Helper');
            //add custom script path for partials
            $view->addScriptPath(APPLICATION_PATH . '/../library/My/View/Scripts/');
            //set css includes, path is relative to /public
            $view->headlink()->setStylesheet('/bootstrap/css/bootstrap.css');
            //add javascript files, path is relative to /public
            $view->headScript()->setFile('/bootstrap/js/jquery.min.js');
            $view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
            //add it to the view renderer
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                    'ViewRenderer');
            $viewRenderer->setView($view);
            //Return it, so that it can be stored by the bootstrap
            return $view;
        }
    

    Hope this provides some help.