2012-07-24 187 views
0

我一直在努力通過Apress出版的書「臨Zend框架技術」。我試圖在視圖上呈現表單。致命錯誤:類「Form_BugReport」未找到

的形式被稱爲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

或者,如果你寧願只是閱讀:致命錯誤:類「Form_BugReport」中找不到的/ home/www數據/ zend.danielgroves.net/htdocs/application/controllers/BugController.php 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 */

+1

解析錯誤是在你的表單類中,而不是你的控制器。您在該行的'this'前面缺少'$'。 – 2012-07-24 13:35:52

+0

謝謝蒂姆,找到並修好了!非常感謝! – 2012-07-24 13:41:01

回答

1

與嘗試:$frmBugReport = new Form_BugReportForm();

您使用了錯誤的類名。

+0

謝謝你的建議,這已經移除了錯誤,但不幸的是用一個新的替換它,看到我的編輯和我原來的問題的底部。感謝您一直以來的幫助! – 2012-07-24 13:29:27

2

你有一個小錯字。該錯誤在您的表單中。您缺少$之前的「本」上線9:好,你的項目的其餘部分運氣,這是一個很不錯的書對於初學者但也有它的幾個小錯誤。查看編輯器網站獲取詳細信息:勘誤部分中的http://www.apress.com/9781430218791/

<?php 

class Form_BugReportForm extends Zend_Form 
{ 
    public function init() 
    { 
     // add element: author textbox 
//Next line is the line to change (add a $ before the "this") 
     $author = $this->createElement('text', 'author'); 
     $author->setLabel('Enter your name:'); 
     $author->setRequired(TRUE); 
     $author->setAttrib('size', 30); 
     $this->addElement($author);