2016-04-21 108 views
1

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

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'), 
))); 

但是,当我打电话$form = new CustomForm();,在usercontroller.php,我得到这个“致命错误路径:类‘CustomForm’未找到在250行的/var/www/demoapp/application/controllers/UserController.php“。可能是什么问题?

+0

'的var_dump(真实路径(APPLICATION_PATH '/../应用/表格'));'并仔细检查,这是你所期望的。 – mkaatman

+0

雅路径是正确的,我得到这个字符串(37)“/ var/www/demoapp/application/forms” – Ninju

+0

哦,等等,我看到发生了什么。您正试图自动加载它。 set_include_path设置可用的路径,但您仍然需要包含/需要这些文件。你可能想要做的是设置自动加载。 http://framework.zend.com/manual/1.12/en/zend.loader.autoloader.html – mkaatman

回答

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