自动加载自定义 Zend_Form_Element

发布于 2024-10-21 08:17:26 字数 1436 浏览 2 评论 0原文

我无法让 Zend 自动加载自定义表单元素类。我完全按照 Marcin 此处 (除了我的课程以“Zend”而不是“my”开头,但我收到此错误:

Warning: include_once(Zend\Form\Element\Div.php) [function.include-once]:无法打开流:没有这样的文件或目录

我在forms\elements\Zend_View_Helper_FormDiv<内有Zend_Form_Element_Div /code> inside views\helpers\

基本上,错误消息中的每个文件夹都缺少一个 's',正确的路径是 Zend\Forms\Elements\Div.php

我的引导程序中也有这个,虽然我不确定是否有必要,但我也将其用于我的表单和模型文件夹(以及其他一些文件夹,但我认为不需要将它们全部发布):

<?php
    $resourceLoader->addResourceTypes(array(
        'model' => array(
        'namespace' => 'Model',
        'path' => 'models'
        ),
        'element' => array(
        'namespace' => 'Element',
        'path' => 'elements'
        ),
        'form' => array(
        'namespace' => 'Form',
        'path' => 'forms'
        )
));
?>

(实际上还有其他方法可以执行此自动加载吗?而不是声明每个文件夹?)

更新:

  • application/forms/elements/Div.php 中的 Element_Div
  • 在我的表单 init() 方法中: $this->addElementPrefixPath ('Element_', APPLICATION_PATH . '/forms/elements');
  • 我收到错误:致命错误:在第 63 行的 C:\xampplite\htdocs\code\application\forms\PostForm.php 中找不到类“Element_Div”

I can't get Zend to autoload a custom form element class. I did things exactly as Marcin describes here (except that my classes start with 'Zend' and not 'my' but I'm getting this error:

Warning: include_once(Zend\Form\Element\Div.php) [function.include-once]: failed to open stream: No such file or directory

I have Zend_Form_Element_Div inside forms\elements\ and Zend_View_Helper_FormDiv inside views\helpers\

Basically, every folder in the error message is missng an 's', the right path is Zend\Forms\Elements\Div.php

I also have this in my bootstrap, though I'm not sure if it's necessary, but I'm also using this for my forms and models folder (and some others, but I don't think there's need to post them all):

<?php
    $resourceLoader->addResourceTypes(array(
        'model' => array(
        'namespace' => 'Model',
        'path' => 'models'
        ),
        'element' => array(
        'namespace' => 'Element',
        'path' => 'elements'
        ),
        'form' => array(
        'namespace' => 'Form',
        'path' => 'forms'
        )
));
?>

(Is there actually any other way of doing this autoloading? Instead of declaring every single folder?)

Update:

  • Element_Div in application/forms/elements/Div.php
  • In my forms init() method: $this->addElementPrefixPath('Element_', APPLICATION_PATH . '/forms/elements');
  • Error I'm getting: Fatal error: Class 'Element_Div' not found in C:\xampplite\htdocs\code\application\forms\PostForm.php on line 63

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-10-28 08:17:27

您本质上必须通过以下方式告诉表单在哪里找到自定义元素:

$form->addElementPrefixPath()

在您的情况下,您可以在表单的 < code>init() 或 __construct() 方法 - 类似:

$this->addElementPrefixPath('Zend_Form_Element_', APPLICATION_PATH . '/elements); ;

不过,我同意@Marcin 的观点。使用 Zend_ 伪命名空间命名您自己的类是不明智的。要么:

  1. 在创建 $resourceLoader 时决定应用程序命名空间并在 Bootstrap 中声明它

  2. 创建一个驻留在您的包含路径上的自定义库 - 可能与 Zend 库处于同一级别 - 并放置您的自定义内容

如果您需要有关这些建议的更多详细信息,请告诉我,我会稍微丰富一下解释。

根据评论更新

使用空的应用程序命名空间,对 addElementPrefixPath() 的调用现在更改为:

$this->addElementPrefixPath('Element_', APPLICATION_PATH . ' /elements);

我想您可以从 Bootstrap 中的 $resourceLoader 定义中删除 elements 条目,因为它实际上不是做任何事。

更新 2

我假设您使用短名称将元素添加到表单中,例如:

$form->addElement('div', 'my_div');

在这种情况下,我们需要告诉 $form 及其插件注册表在哪里查找“div”类型的元素。这就是我们处理 $form->addElementPrefixPath() 的原因。

但是,从您报告的错误消息来看,您似乎正在使用类似以下内容将自定义元素添加到表单中:

$div = new Element_Div();
$form->addElement($div, 'my_div');

在这种情况下,它不是 $form 及其插件注册表担心查找/加载/实例化自定义元素;它是通过其 $resourceLoader 实现的 $autoloader。在这种情况下,就不需要 $form->addElementPrefixPath(),它本质上是对表单的提示,提示如何查找短名称调用的自定义元素。

我们需要在 Bootstrap 中配置 $resourceLoader,以便它知道在哪里可以找到该类。假设您坚持使用空的应用程序命名空间(因此您的类名为 Element_Div),并将文件放置在 application/forms/elements/Div.php 中,则 $ resourceLoader 调用如下:

$resourceLoader->addResourceTypes(array(
    'model' => array(
        'namespace' => 'Model_',
        'path' => 'models'
    ),
    'element' => array(
        'namespace' => 'Element_',
        'path' => 'forms/elements'
    ),
    'form' => array(
        'namespace' => 'Form_',
         'path' => 'forms'
    )
));

应该可以了。 【著名的遗言,嗯?】

You essentially have to tell the form where to find custom elements by using:

$form->addElementPrefixPath()

In your case, you would use - either within the form's init() or __construct() method - something like:

$this->addElementPrefixPath('Zend_Form_Element_', APPLICATION_PATH . '/elements);;

However, I have agree with @Marcin. Naming your own classes with the Zend_ pseudo-namespace is ill-advised. Either:

  1. Decide on an application namespace and declare it in your Bootstrap when you create your $resourceLoader

  2. Create an custom library that resides on your include path - probably at the same level as the Zend library - and put your custom stuff out there.

Let me know if you need more details on either of these suggestions and I'll fatten up the explanations a bit.

Update based on comments

Using an empty appnamespace, your call to addElementPrefixPath() now changes to:

$this->addElementPrefixPath('Element_', APPLICATION_PATH . '/elements);

And I guess you could remove the elements entry from the $resourceLoader definition in your Bootstrap since it's really not doing anything.

Update 2

I assumed that you were adding the element to the form using the shortname, something like:

$form->addElement('div', 'my_div');

In this circumstance, we need to tell the $form and its plugin registry where to find an element of type 'div'. That's why we dealt with $form->addElementPrefixPath().

However, from the error message you are reporting, it appears that you are adding your custom element to the form using something like:

$div = new Element_Div();
$form->addElement($div, 'my_div');

In this case, it is not the $form and its plugin registry that has to worry about finding/loading/instantiating the custom element; it is the $autoloader via its $resourceLoader. In that case, there is no need for the $form->addElementPrefixPath(), which is essentially a hint to the form on how to find custom elements invoked by shortname.

What we need is to configure the $resourceLoader back in Bootstrap so it knows where to find the class. Assuming you stick with empty appnamespace (so your class is named Element_Div) and you place the file in application/forms/elements/Div.php, then the $resourceLoader call is as follows:

$resourceLoader->addResourceTypes(array(
    'model' => array(
        'namespace' => 'Model_',
        'path' => 'models'
    ),
    'element' => array(
        'namespace' => 'Element_',
        'path' => 'forms/elements'
    ),
    'form' => array(
        'namespace' => 'Form_',
         'path' => 'forms'
    )
));

That should do it. [Famous last words, eh?]

故人的歌 2024-10-28 08:17:27

我更喜欢创建这样的表单:

$form->addElement(new My_Form_Element_Whatever(array(
    'name' => 'my_element',
    'label' => 'My element',
)));

或者

$form->addElement($whatever = new My_Form_Element_Whatever(array(
    'name' => 'my_element',
    'label' => 'My element',
)));
$whatever->removeDecorator('Errors');

当我需要进一步修改元素时。

I prefer creating forms like this:

$form->addElement(new My_Form_Element_Whatever(array(
    'name' => 'my_element',
    'label' => 'My element',
)));

or

$form->addElement($whatever = new My_Form_Element_Whatever(array(
    'name' => 'my_element',
    'label' => 'My element',
)));
$whatever->removeDecorator('Errors');

when I need to further modify the element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文