Rob Allen的Zend Framework 2教程:PHP致命错误:未找到Class'Album \ AlbumTable'

时间:2012-09-03 18:33:09

标签: zend-framework zend-framework2

我希望有人能帮助我。

我正在通过Rob Allen's Zend Framework 2 Tutorial

当我试图去:

http://localhost/album/index

我收到以下错误:

[03-Sep-2012 17:48:07 UTC] PHP Fatal error:  Class 'Album\AlbumTable' not found in C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\Module.php on line 33
[03-Sep-2012 17:48:07 UTC] PHP Stack trace:
[03-Sep-2012 17:48:07 UTC] PHP   1. {main}() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public\index.php:0
[03-Sep-2012 17:48:07 UTC] PHP   2. Zend\Mvc\Application->run() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public\index.php:12
[03-Sep-2012 17:48:07 UTC] PHP   3. Zend\EventManager\EventManager->trigger() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php:298
[03-Sep-2012 17:48:07 UTC] PHP   4. Zend\EventManager\EventManager->triggerListeners() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:209
[03-Sep-2012 17:48:07 UTC] PHP   5. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP   6. Zend\Mvc\DispatchListener->onDispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP   7. Zend\Mvc\Controller\AbstractController->dispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php:114
[03-Sep-2012 17:48:07 UTC] PHP   8. Zend\EventManager\EventManager->trigger() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php:108
[03-Sep-2012 17:48:07 UTC] PHP   9. Zend\EventManager\EventManager->triggerListeners() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:209
[03-Sep-2012 17:48:07 UTC] PHP  10. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP  11. Zend\Mvc\Controller\AbstractActionController->onDispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP  12. Album\Controller\AlbumController->indexAction() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php:87
[03-Sep-2012 17:48:07 UTC] PHP  13. Album\Controller\AlbumController->getAlbumTable() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:26
[03-Sep-2012 17:48:07 UTC] PHP  14. Zend\ServiceManager\ServiceManager->get() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:18
[03-Sep-2012 17:48:07 UTC] PHP  15. Zend\ServiceManager\ServiceManager->create() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:412
[03-Sep-2012 17:48:07 UTC] PHP  16. Zend\ServiceManager\ServiceManager->createFromFactory() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:455
[03-Sep-2012 17:48:07 UTC] PHP  17. Zend\ServiceManager\ServiceManager->createServiceViaCallback() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:801
[03-Sep-2012 17:48:07 UTC] PHP  18. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:684
[03-Sep-2012 17:48:07 UTC] PHP  19. Album\Module->Album\{closure}() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:684

请注意,即使AlbumTable.php位于相册\模型中,也说 Class'Album \ AlbumTable'找不到

模块\相册\ Module.php:

<?php

// module/Album/Module.php
namespace Album;

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(
                'Album\Model\AlbumTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new AlbumTable($dbAdapter);
                    return $table;
                },
            ),
        );
    }
}

模块\相册\ SRC \相册\型号\ AlbumTable.php:

<?php

// module/Album/src/Album/Model/AlbumTable.php:
namespace Album\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;

class AlbumTable extends AbstractTableGateway
{
    protected $table ='album';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Album());
        $this->initialize();
    }

    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }

    public function getAlbum($id)
    {
        $id  = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function saveAlbum(Album $album)
    {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );
        $id = (int)$album->id;
        if ($id == 0) {
            $this->insert($data);
        } else {
            if ($this->getAlbum($id)) {
                $this->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteAlbum($id)
    {
        $this->delete(array('id' => $id));
    }
}

模块\相册\ SRC \相册\控制器\ AlbumController.php:

<?php

// module/Album/src/Album/Controller/AlbumController.php:
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{

    protected $albumTable;

    public function getAlbumTable()
    {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

    public function indexAction()
    {
        return new ViewModel(array(
            'albums' => $this->getAlbumTable()->fetchAll(),
        ));
    }

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }
}

我的配置:

  • Windows 7。
  • Zend Server CE技术预览版(PHP 5.4)(注意我已经尝试过使用Zend Server CE(PHP 5.3)但是得到了同样的错误)。
  • Apache和MySQL适用于其他PHP页面,例如我可以使用简单的PHP脚本中的PDO连接到“相册”表。

你们有谁知道问题可能是什么?

2 个答案:

答案 0 :(得分:4)

类为Album\Model\AlbumTable,但错误消息为Album\AlbumTable

堆栈跟踪引导您进入ServiceManager。在Album\Model\AlbumTables

中将Album\Module::getServiceConfig()的关闭添加到ServiceManager

当命名空间$table = new AlbumTable($dbAdapter);解析为Album时,您可以看到Album\AlbumTable
这很容易解决:在名称空间声明

之后将use Album\Model\AlbumTable;添加到module \ Album \ Module.php

答案 1 :(得分:1)

替换你的行
$table = new \Album\Model\AblumTable($dbAdapter);

或者在课前在文件顶部添加一个用途。

use Album\Model\AlbumTable;