【问题标题】:ZendFramework 2.0 Php Fatal Error: Uncaught Error: Class not foundZendFramework 2.0 Php 致命错误:未捕获错误:找不到类
【发布时间】:2026-02-21 05:35:01
【问题描述】:

我已经看到了一些关于这个方向的问题,但没有一个对我的案例有实际帮助。

PHP 致命错误:未捕获的错误:在 /var/www/html/module/BookList/Module.php:40 中找不到类“Booklist\Model\BookTable”\

这是我从/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);

也许这里的任何人都知道我能做什么。

【问题讨论】:

    标签: php class zend-framework2 fatal-error


    【解决方案1】:

    Module.php:

    use Booklist\Model\BookTable;
    

    应该是:

    use BookList\Model\BookTable;
    

    (注意大写“L”)。

    【讨论】:

      最近更新 更多