2017-07-28 232 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')。

相关问题