2017-07-28 225 views
1

我已經在這個方向看到了一些問題,但沒有一個真正幫助我的情況。ZendFramework 2.0 Php致命錯誤:未捕獲錯誤:未找到類

PHP Fatal error: Uncaught Error: Class 'Booklist\Model\BookTable' not found in /var/www/html/module/BookList/Module.php:40\

這是我從我的/var/log/apache2/error.log中提取的錯誤。

我已經找了同樣的問題,但我找不到解決方案。我已經包含了文件和命名空間,但我顯然無法訪問該文件。

我正在與zenframework 2.2.10

這是我的文件結構的外觀:

module 
-Application 
-BookList 
    -config 
     \module.config.php 
    -src 
    -BookList 
     -Controller 
      \BookController.php 
     -Form 
      \BookForm.php 
     -Model 
      \Book.php 
      \BookTable.php 
    -view 
     -book-list 
     -book 
      \.. 
    \autoload_classmap.php 
    \module.php 

首先這是我Module.php

namespace BookList; 

use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

use Booklist\Model\BookTable; 
use BookList\Model\Book; 

class Module{ 
    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\ClassMapAutoloader' => array(
       __DIR__ . '/autoload_classmap.php', 
      ), 
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function getConfig() 
    { 
     return include __DIR__ . '/config/module.config.php'; 
    } 
    public function getServiceConfig(){ 
     return array(
      'factories' => array(
       'BookList\Model\BookTable' => function($sm){ 
        $tableGateway = $sm->get('BookTableGateway'); 
        $table = new BookTable($tableGateway); 

        return $table; 
       }, 
       'BookTableGateway' => function($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new 
Book()); 
        return new 
TableGateway('book',$dbAdapter,null,$resultSetPrototype); 
       }, 
      ), 
     ); 
    } 
} 

這是我BookTable.php

namespace BookList\Model; 

use Zend\Db\TableGateway\TableGateway; 

class BookTable { 
    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) 
    { 
     $this->tableGateway = $tableGateway; 
    } 
    public function fetchAll(){ 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 
    public function getBook($id) { 
     $id = (int) $id; 
     $rowset = $this->tableGateway->select(array('id'=>$id)); 
     $row = $rowset->current(); 
     if(!$row) { 
      throw new \Exception("Could not find row $id"); 
     } 
     return $row; 
    } 
    public function saveBook(Book $book){ 
     $data = array(
      'title' => $book->title, 
      'author' => $book->author, 
     ); 

     $id = (int) $book->id; 
     if($id == 0){ 
      $this->tableGateway->insert($data); 
     }else{ 
      if($this->getBook($id)){ 
       $this->tableGateway->update($data,array('id'=>$id)); 
      } 
      else{ 
       throw new \Exception("Book id does not exist"); 
      } 
     } 
    } 
    public function deleteBook($id){ 
     $this->tableGateway->delete(array('id'=>(int) $id)); 
    } 

} 

最後,這個我我的BookController.php

namespace BookList\Controller; 
use BookList\Model\Book; 

use BookList\Form\BookForm; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

class BookController extends AbstractActionController{ 
    protected $bookTable; 

    public function indexAction() 
    { 
     return new ViewModel(array(
      'books' => $this->getBookTable()->fetchAll(), 
     )); 
    } 
    public function addAction() 
    { 
     $form = new BookForm(); 
     $form->get('submit')->setValue('Add'); 

     $request = $this->getRequest(); 
     if($request->isPost()){ 

     } 
     return array('form'=>$form); 
    } 

    public function editAction() 
    { 
     $form = new BookForm(); 
     //$book->bind($book); 
     $form->get('submit')->setAttribute('value','Edit'); 
     $request = $this->getRequest(); 
     if($request->isPost()){ 

     } 
     return array(
      'id' => $id, 
      'form' => $form, 
     ); 
    } 

    public function deleteAction() 
    { 
     $id = (int) $this->params()->fromRoute('id',0); 
     if(!$id){ 
      return $this->redirect()->toRoute('book'); 
     } 

     $request = $this->getRequest(); 
     if($request -> isPost()){ 

     } 
     return array(
      'id' => $id, 
      //'book' => , 
     ); 
    } 
    public function getBookTable(){ 
     if(!$this->bookTable){ 
      $sm = $this->getServiceLocator(); 
      $this->bookTable = $sm->get('BookList\Model\BookTable'); 
     } 
     return $this->bookTable; 
    } 
} 

的錯誤,當我試圖調用這個類的原因:

$table = new BookTable($tableGateway); 

也許有人在這裏有一個想法,我能做些什麼。

回答

1

Module.php

use Booklist\Model\BookTable; 

應該是:

use BookList\Model\BookTable; 

(注意是大寫的 'L')。

相關問題