Zend:自定义视图助手

时间:2013-04-29 12:17:15

标签: zend-framework zend-view

我正在尝试在我的项目中添加一个View帮助器,但是我收到以下错误:

[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

应用/视图/助手/ LoggedInAs.php

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

应用/ CONFIGS /的application.ini

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

应用/布局/脚本/ layout.phtml

echo $this->loggedInAs();

stackoverflow还有其他几个问题,但这些问题对我来说都不起作用。

编辑1: Tim Fountain回答后,将Zend_View_Helper_LoggedInAs更改为My_View_Helper_LoggedInAs 编辑2: 完整错误

3 个答案:

答案 0 :(得分:3)

您提供的答案似乎没有多大意义,如果您说您将<?php添加到编写您的课程的文件的顶部,我只能说“欢迎使用PHP!” 否则以下可能会有所帮助......或者不是。

使用feibeck中的主application.ini作为参考,我想出了:

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

但是你使用MVC的默认值,所以你根本不需要任何配置。

这可能是配置太多的情况。

这就是说: 我从来不喜欢在application.ini中设置视图选项,因为我从来不确定我应该期待什么效果(我是在添加还是设置选项?)。我更喜欢在bootstrap中设置视图,因为大多数使用的方法都比较冗长,并且讲述了一个更完整的故事:

//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;
    }

希望这能提供一些帮助。

答案 1 :(得分:0)

类名应为My_View_Helper_LoggedInAs,因为这是您在application.ini中声明的命名空间。 Zend名称空间仅适用于ZF类。

答案 2 :(得分:0)

我用一个奇怪的解决方案修复它,因为我在互联网上找不到这样的东西。 我刚刚在 application / views / helpers / LoggedInAs.php

的开头添加了<?PHP
相关问题