2008年9月28日星期日

Re: [fw-mvc] Zend Autoloader and forms

-- Tom Printy <tprinty@mail.edisonave.net> wrote
(on Sunday, 28 September 2008, 07:14 PM -0500):
> Hello I have a question about zend form and the autoloader.
>
> I have created a directory application/forms to house all my forms
>
> However the AutoLoader cannot find the forms directory. I still have to
> do something like
>
> require_once '../application/forms/LoginForm.php';
> $form = new Form_Login();
>
>
> Is there a shortcut I can take so Autoloader can take care of this form
> me?

Currently, no, as it follows a different naming convention than the one
used by Zend_Loader.

You have two principal options here:

* put your forms in a library directory, and namespace them:

application/
library/
My/
Form/
Login.php (contains My_Form_Login)
Zend/

This will allow autoloading to work, but puts your forms outside your
application/ tree.

* use a plugin loader inside an action helper for loading forms. You
can see an example of this in the bugapp I created for a workshop at
the Dutch PHP Conference:

http://github.com/weierophinney/bugapp/tree/master/application/controllers/helpers/GetForm.php

Usage would then be:

$form = $this->_helper->getForm('login');

which takes care of the name resolution, file resolution, and
object instantiation. This is slightly more work -- but if the
ResourceLoader action helper is finished for 1.7, you'll be able to
use that instead.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: