我一直在研究 Apress 的书“Pro Zend Framework Techniques”。我已经达到了尝试在视图上呈现表单的地步。
表单名为BugReport
,位于forms/BugReportForm.php
; 这是文件的内容:
<?php
class Form_BugReportForm extends Zend_Form
{
public function init()
{
// add element: author textbox
$author = this->createElement('text', 'author');
$author->setLabel('Enter your name:');
$author->setRequired(TRUE);
$author->setAttrib('size', 30);
$this->addElement($author);
// add element: email textbox
$email = $this->createElement('text', 'email');
$email->setLabel('Your email address:');
$email->setRequired(TRUE);
$email->addValidator(new Zend_Validate_EmailAddress());
$email->addFilters(array(
new Zend_Filter_StringTrim(),
new Zend_Filter_StringToLower()
));
$email = setAttrib('size', 40);
$this->addElement($email);
// add element: date textbox
$date = $this->createElement('text', 'date');
$date->setLabel('Date the issue occurred (dd-mm-yyyy)');
$date->setRequired(TRUE);
$date->addValidator(new Zend_Validate_Date('MM-DD-YYYY'));
$date->setAttrib('size', 20);
$this->addElement($date);
// add element: URL textbox
$url = $this->createElement('text', 'url');
$url->setLabel('Issue URL:');
$url->setRequired(TRUE);
$url->setAttrib('size', 50);
$this->addElement($url);
// add element: description text area
$description = $this->createElement('textarea', 'description');
$description->setLabel('Issue description:');
$description->setRequired(TRUE);
$description->setAttrib('cols', 50);
$description->setAttrib('rows', 4);
$this->addElement($description);
// add element: priority select box
$priority = $this->createElement('select', 'priority');
$priority->setLabel('Issue priority:');
$priority->setRequired(TRUE);
$priority->addMultiOptions(array(
'low' => 'Low',
'med' => 'Medium',
'high' => 'High'
));
$this->addElement($priority);
// add element: status select box
$status = $this->createElement('select', 'status');
$status->setLabel('Current status:');
$status->setRequired(TRUE);
$status->addMultiOptions(array(
'new' => 'New',
'in_progress' => 'In Progress',
'resolved' => 'Resolved'
));
$this->addElement($status);
// add element: submit button
$this->addElement('submit', 'submit', array('label' => 'Submit'));
}
}
这种形式通过BugController
位于的控制器控制/controllers/BugController.php
<?php
class BugController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function createAction()
{
// action body
}
public function submitAction()
{
$frmBugReport = new Form_BugReport();
$frmBugReport->setAction('/bug/submit');
$frmBugReport->setMethod('post');
$this->view->form = $frmBugReport();
}
}
最后,我的引导程序中有以下自动加载器。
protected function _initAutoload()
{
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_',
)
),
));
// Return it so it can be stored by the bootstrap
return $autoLoader;
}
我得到的错误可以在这里看到:http: //zend.danielgroves.net/bug/submit
或者,如果您只想阅读它:Fatal error: Class 'Form_BugReport' not found in /home/www-data/zend.danielgroves.net/htdocs/application/controllers/BugController.php on line 23
关于我做错了什么的任何想法,以及如何解决这个问题?
编辑
ArneRie 指出我使用了错误的类名,但不幸的是,这并没有解决问题。这是 BugController 现在的样子:
<?php
class BugController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function createAction()
{
// action body
}
public function submitAction()
{
$frmBugReport = new Form_BugReportForm();
$frmBugReport->setAction('/bug/submit');
$frmBugReport->setMethod('post');
$this->view->form = $frmBugReport;
}
}
尽管这确实消除了有问题的错误,但一个新的错误已经取代了它。
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/www-data/zend.danielgroves.net/htdocs/application/forms/BugReportForm.php on line 8
第8行很有趣/* Initialize action controller here */