【问题标题】:Zend 1.11, Decorator file locationZend 1.11,装饰器文件位置
【发布时间】:2013-07-26 14:20:06
【问题描述】:

我到处搜索,但找不到答案,所以问题是,装饰器类必须在哪里,让我们看一个例子:

我有 application/forms/Guestbook.php

class Application_Form_Guestbook extends Zend_Form
{

    public function init()
    {

        $this->addPrefixPath('My_Decorator', '/application/forms/decorator', 'decorator');

        $this->setMethod('post');
        $this->addElement('text', 'email', array(
            'label' => 'Your email address',
            'require' => true,
            'filters' => array('StringTrim'),
            'validators' => array('EmailAddress'),
            //'prefixPath' => array('decorator' => array('My_Decorator' => 'application/forms/decorator')),
            'decorators' => array(array('SimpleInput'))));



        // Add the comment element
        $this->addElement('textarea', 'comment', array(
            'label' => 'Please Comment:',
            'required' => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(0, 20))
            )
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'label' => 'Please enter the 5 letters displayed below:',
            'required' => true,
            'captcha' => array(
                'captcha' => 'Figlet',
                'wordLen' => 5,
                'timeout' => 300
            )
        ));

        $this->addElement('submit', 'submit', array('ignore' => true, 'label' => 'Sign Guestbook'));
        $this->addElement('hash', 'csrf', array('ignore' => true));
    }

}

并拥有 application/forms/decorator/SimpleInput.php

class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
{

    protected $_format = '<label for="%s">%s !!!!!!</label><input id="%s" name="%s" type="text" value="%s"/>';

    public function render($content)
    {
        $element = $content->getElement();
        $name = htmlentities($element->getFullyQualifiedName());
        $label = htmlentities($element->getLabel());
        $id = htmlentities($element->getId());
        $value = htmlentities($element->getValue());

        $markup = sprintf($this->_format, $name, $label, $id, $name, $value);

        return $markup;
    }

}

当我尝试启动页面时,我在加载装饰器时出错:

An error occurred
Application error
Exception information:

Message: Plugin by name 'SimpleInput' was not found in the registry; used paths: My_Decorator_: /application/forms/decorator/ Zend_Form_Decorator_: Zend/Form/Decorator/
Stack trace:

#0 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(1827): Zend_Loader_PluginLoader->load('SimpleInput')
#1 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(2207): Zend_Form_Element->_getDecorator('SimpleInput', NULL)
#2 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(1980): Zend_Form_Element->_loadDecorator(Array, 'SimpleInput')
#3 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(316): Zend_Form_Element->getDecorators()
#4 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(271): Zend_Form_Element->loadDefaultDecorators()
#5 /home/dev/public_html/local.zend.test/library/Zend/Form.php(1125): Zend_Form_Element->__construct('email', Array)
#6 /home/dev/public_html/local.zend.test/library/Zend/Form.php(1035): Zend_Form->createElement('text', 'email', Array)
#7 /home/dev/public_html/local.zend.test/application/forms/Guestbook.php(18): Zend_Form->addElement('text', 'email', Array)
#8 /home/dev/public_html/local.zend.test/library/Zend/Form.php(240): Application_Form_Guestbook->init()
#9 /home/dev/public_html/local.zend.test/application/controllers/GuestbookController.php(28): Zend_Form->__construct()
#10 /home/dev/public_html/local.zend.test/library/Zend/Controller/Action.php(516): GuestbookController->signAction()
#11 /home/dev/public_html/local.zend.test/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('signAction')
#12 /home/dev/public_html/local.zend.test/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#13 /home/dev/public_html/local.zend.test/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#14 /home/dev/public_html/local.zend.test/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#15 /home/dev/public_html/local.zend.test/public/index.php(26): Zend_Application->run()
#16 {main}  

此示例来自快速指南。问题是这些装饰器的位置必须在哪里(默认位置),或者我如何加载它们以及我应该写哪个“路径/到/装饰器”(从根\应用程序\表单文件夹?),因为 Zend 中的文档通常很难看。 .. =\

【问题讨论】:

    标签: zend-framework zend-decorators


    【解决方案1】:

    编辑#3。

    我认为问题可能在于 Zend 不喜欢您设置路径的方式。 /application 是一个绝对路径,可能无法解决您认为的问题。

    试试:

    $this->addPrefixPath('My_Decorator', APPLICATION_PATH . '/forms/decorator', 'decorator');
    

    这样路径一定是好的。 Zend 很可能正在 /public/application/etc... 中查找您使用它的方式。

    希望对您有所帮助!

    【讨论】:

    • 不,它没有。 :(我已经尝试过这种方式并玩了很多但找不到解决方案。
    • 用另一个“应该”工作的解决方案编辑了我的答案,除非你的设置中有我不知道的奇怪或特殊的东西。
    • 所以你不会说只有这样装饰器才能存在于 library/{myFolder}/... (我是 zend 的新手,所以问题可能是假的)
    • 在更好地查看了您的问题后再次编辑了我的答案。错过了你的路径问题。
    【解决方案2】:

    装饰器和其他类一样使用Autoloader加载,所以装饰器需要按照类名来放置:

    例如。类My_Decorator_Somethinglibrary/My/Decorator/Something.php

    在这种情况下,最好的选择似乎是改进您的装饰器类命名。

    但是,如果您需要其他自定义位置,则需要编写并注册您自己的自动加载器。

    【讨论】:

      猜你喜欢
      • 2012-03-02
      • 2011-01-20
      • 2011-11-26
      • 2013-07-25
      • 2011-01-10
      • 1970-01-01
      • 2012-03-18
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多