Zend表单自动加载

时间:2016-04-21 12:26:27

标签: php zend-framework

我在Zend框架中实现表单时遇到问题1.我在application / forms / CustomForm.php中添加了表单。它看起来像这样。

class CustomForm extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $id = $this->createElement('hidden','id');
        $firstname = $this->createElement('text','firstname');
        $firstname->setLabel('First Name:')
                    ->setAttrib('size',50);
        $lastname = $this->createElement('text','lastname');
        $lastname->setLabel('Last Name:')
                ->setAttrib('size',50);
        $username = $this->createElement('text','username');
        $username->setLabel('Username:')
                ->setAttrib('size',50);
        $email = $this->createElement('text','email');
        $email->setLabel('Email:')
                ->setAttrib('size',50);
        $password = $this->createElement('password','password');
        $password->setLabel('Password:')
                    ->setAttrib('size',50);

        $password2 = $this->createElement('password','password2');
        $password2->setLabel('Confirm Password::')
                    ->setAttrib('size',50);
        $register = $this->createElement('submit','register');
        $register->setLabel("Register")
                ->setIgnore(true);

        $this->addElements(array(
            $firstname,
            $lastname,
            $username,
            $email,
            $password,
            $password2,
            $id,
            $register
        ));
    }
}

并在index.php中包含路径,如下所示

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../application'),
    realpath(APPLICATION_PATH . '/../application/forms'),
)));

但是当我在usercontroller.php中调用$form = new CustomForm();时,我得到了这个“致命错误:在/var/www/demoapp/application/controllers/UserController.php中找不到类'CustomForm' 250“。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

在自动加载器中注册表单类不是更好吗? http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html

您也可以在application / Bootstrap.php文件中执行此操作,STH LIKE ::

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initResourceAutoloader()
    {
        $autoLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'  => dirname(__FILE__),
            'namespace' => 'App'
        ));

        $autoLoader->addResourceType('form'     , 'forms'       , 'Form');'Grid');
    return $autoLoader;
}