社区早上好,我需要帮助在Zend Frameword 3中按照教程3创建Album模块。我有以下错误:
<?php
[ModelAlbumTable::class =>
function ($container)
{
$tableGateway = $container->get(ModelAlbumTableGateway::class);
return new ModelAlbumTable($tableGateway);
}
, ModelAlbumTableGateway::class =>
function ($container)
{
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new ModelAlbum());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
}
, ], ];
} // Add this method:
public function getControllerConfig()
{ return [ 'factories' => [ Controller\AlbumController::class =>
function($container) {
return new Controller\AlbumController( $container-
get(Model\AlbumTable::class) ); }, ], ]; } }
?>
错误如下:
致命错误:未捕获Zend \ ModuleManager \ Exception \ RuntimeException:无法初始化模块(相册)。在C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-modulemanager \ src \ ModuleManager.php:203堆栈跟踪:#0 C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-modulemanager \ src \ ModuleManager .php(175):Zend \ ModuleManager \ ModuleManager-&gt; loadModuleByName(Object(Zend \ ModuleManager \ ModuleEvent))#1 C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-modulemanager \ src \ ModuleManager.php( 97):Zend \ ModuleManager \ ModuleManager-&gt; loadModule('Album')#2 C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-eventmanager \ src \ EventManager.php(322):Zend \ ModuleManager \ ModuleManager - &gt; onLoadModules(Object(Zend \ ModuleManager \ ModuleEvent))#3 C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-eventmanager \ src \ EventManager.php(171):Zend \ EventManager \ EventManager-&gt; triggerListeners(Object(Zend \ ModuleManager \ ModuleEvent))#4 C:\ xampp \ htdocs \ marycela \ vendor \ zendframework \ zend-modulemanager \ src \ ModuleManager.php(120):Zend \ EventManager \ Event in C:\ xampp \ htdocs中\ marycela \厂商\ zendframework \ Zend的模块第203行经理\ src \ ModuleManager.php
module.config.php
namespace Album;
use Zend\Router\Http\Segment;
return
[
'router' =>
[
'routes' =>
[
'album' =>
[
'type' => Segment::class,
'options' =>
[
'route' => '/album[/:action[/:id]]',
'constraints' =>
[
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' =>
[
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
]
],
],
],
'view_manager' =>
[
'template_path_stack' =>
[
'album' => __DIR__ . '/../view',
],
],
];
modules.confing.php
return [
'Zend\ServiceManager\Di',
'Zend\Session',
'Zend\Log',
'Zend\Form',
'Zend\Db',
'Zend\Cache',
'Zend\Router',
'Zend\Validator',
'Application',
'Album',
];
Album.Controller.phph
namespace Album\Controller;
use Album\Model\AlbumTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
// Add this property:
private $table;
// Add this constructor:
public function __construct(AlbumTable $table)
{
$this->table = $table;
}
public function indexAction()
{
return new ViewModel([
'albums' => $this->table->fetchAll(),
]);
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
AlbumTable.php
namespace Album\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class AlbumTable
{
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
public function getAlbum($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(['id' => $id]);
$row = $rowset->current();
if (! $row)
{
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
public function saveAlbum(Album $album)
{
$data =
[
'artist' => $album->artist,
'title' => $album->title,
];
$id = (int) $album->id;
if ($id === 0)
{
$this->tableGateway->insert($data);
return;
}
if (! $this->getAlbum($id))
{
throw new RuntimeException(sprintf(
'Cannot update album with identifier %d; does not exist',
$id
));
}
$this->tableGateway->update($data, ['id' => $id]);
}
public function deleteAlbum($id)
{
$this->tableGateway->delete(['id' => (int) $id]);
}
}