我已经在这个方向上看到了一些问题,但没有一个问题在我的情况下实际上有所帮助。
PHP致命错误:未捕获错误:类' Booklist \ Model \ BookTable'在/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);
也许这里的任何人都知道我能做些什么。
答案 0 :(得分:1)
在Module.php
:
use Booklist\Model\BookTable;
应该是:
use BookList\Model\BookTable;
(注意首都' L')。