在此处输入图像描述我试图在Zend Skeleton应用程序中添加一个名为Album
的新模块,但通过URL访问该模块时,会在下面抛出一个错误。我花了将近5个小时,但没有找到任何解决办法。任何帮助都将不胜感激。
文件:/var/www/zf2 tutorial/vendor/zendframework/zend servicemanager/src/Factory/InvokableFactory。菲律宾:30
消息:找不到类Album/Controller/AlbumController。
堆栈跟踪#0/var/www/zf2 tutorial/vendor/zendframework/zend-servicemanager/src/servicemanager。php(758):Zend\ServiceManager\Factory\InvokableFactory-\uu invoke(对象(Zend\ServiceManager\ServiceManager),'Album\Controlle…',NULL)#1/var/www/zf2 tutorial/vendor/zendframework/zend-servicemanager/src/servicemanager。php(200):Zend\ServiceManager\ServiceManager-
**module.config.php**
<?php
namespace Album;
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => Controller\AlbumController::class
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'defaults' => array(
'controller' => Controller\Album::class,
'action' => 'index',
),
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
?>
**AlbumController.php**
<?php
namespace Album;
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' => '',
));
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
**Album.php**
<?php
namespace Album\Model;
class Album
{
public $id;
public $artist;
public $title;
public function exchangeArray($data)
{
$this->id = (! empty($data['id'])) ? $data['id'] : null;
$this->artist = (! empty($data['artist'])) ? $data['artist'] :
null;
$this->title = (! empty($data['title'])) ? $data['title'] : null;
}
}
**AlbumTable.php**
<?php
use Zend\Db\TableGateway\TableGateway;
class AlbumTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getAlbum($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 saveAlbum(Album $album)
{
$data = array(
'artist' => $album->artist,
'title' => $album->title
);
$id = (int) $album->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAlbum($id)) {
$this->tableGateway->update($data, array(
'id' => $id
));
} else {
throw new \Exception('Album id does not exist');
}
}
}
public function deleteAlbum($id)
{
$this->tableGateway->delete(array(
'id' => (int) $id
));
}
}
**Module.php**
<?php
namespace Album;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
u se Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module implements AutoloaderProviderInterface,
ConfigProviderInterface
{
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) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new
Album());
return new TableGateway('album', $dbAdapter, null,
$resultSetPrototype);
}
)
);
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container)
{
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
}
?>
**composer.json**
{
"name" : "zendframework/skeleton-application",
"description" : "Skeleton Application for Zend Framework zend-mvc
applications",
"type" : "project",
"license" : "BSD-3-Clause",
"keywords" : [
"framework",
"mvc",
"zf"
],
"homepage" : "http://framework.zend.com/",
"minimum-stability" : "dev",
"prefer-stable" : true,
"require" : {
"php" : "^5.6 || ^7.0",
"zendframework/zend-component-installer" : "^1.0 || ^0.7 ||
^1.0.0-dev@dev",
"zendframework/zend-mvc" : "^3.0.1",
"zfcampus/zf-development-mode" : "^3.0"
},
"autoload" : {
"psr-4" : {
"Application\\" : "module/Application/src/"
}
},
"autoload-dev" : {
"psr-4" : {
"ApplicationTest\\" : "module/Application/test/"
}
},
"extra" : {
"zend-skeleton-installer" : [{
"name" : "zendframework/zend-developer-tools",
"constraint" : "^1.1.0",
"prompt" : "Would you like to install the developer
toolbar?",
"module" : true,
"dev" : true
}, {
"name" : "zendframework/zend-cache",
"constraint" : "^2.7.1",
"prompt" : "Would you like to install caching support?",
"module" : true
}, {
"name" : "zendframework/zend-db",
"constraint" : "^2.8.1",
"prompt" : "Would you like to install database support
(installs zend-db)?",
"module" : true
}, {
"name" : "zendframework/zend-mvc-form",
"constraint" : "^1.0",
"prompt" : "Would you like to install forms support?",
"module" : true
}, {
"name" : "zendframework/zend-json",
"constraint" : "^3.0",
"prompt" : "Would you like to install JSON
de/serialization support?"
}, {
"name" : "zendframework/zend-log",
"constraint" : "^2.9",
"prompt" : "Would you like to install logging support?",
"module" : true
}, {
"name" : "zendframework/zend-mvc-console",
"constraint" : "^1.1.10",
"prompt" : "Would you like to install MVC-based console
support? (We recommend migrating to zf-console, symfony/console, or
Aura.CLI)",
"module" : true
}, {
"name" : "zendframework/zend-mvc-i18n",
"constraint" : "^1.0",
"prompt" : "Would you like to install i18n support?",
"module" : true
}, {
"name" : "zendframework/zend-mvc-plugins",
"constraint" : "^1.0.1",
"prompt" : "Would you like to install the official MVC
plugins, including PRG support, identity, and flash messages?",
"module" : true
}, {
"name" : "zendframework/zend-psr7bridge",
"constraint" : "^0.2.2",
"prompt" : "Would you like to use the PSR-7 middleware
dispatcher?"
}, {
"name" : "zendframework/zend-session",
"constraint" : "^2.7.1",
"prompt" : "Would you like to install sessions support?",
"module" : true
}, {
"name" : "zendframework/zend-test",
"constraint" : "^3.0.1",
"prompt" : "Would you like to install MVC testing
support?",
"dev" : true
}, {
"name" : "zendframework/zend-servicemanager-di",
"constraint" : "^1.0",
"prompt" : "Would you like to install the zend-di
integration for zend-servicemanager?",
"module" : true
}
]
},
"scripts" : {
"cs-check" : "phpcs",
"cs-fix" : "phpcbf",
"development-disable" : "zf-development-mode disable",
"development-enable" : "zf-development-mode enable",
"development-status" : "zf-development-mode status",
"post-create-project-cmd" : "@development-enable",
"serve" : "php -S 0.0.0.0:8080 -t public public/index.php",
"test" : "phpunit"
}
}
您的配置中存在几个问题。
您应该更改注册控制器的方式。它现在只注册为具有给定字符串的可调用文件。当您尝试在路由配置中使用它时,它未被其FQCN注册。然而,您在路由配置中使用了错误的FQCN。您正在使用Controller\Album::class
,但该类被称为:AlbumController
,因此将其更改为Controller\AlbumController::class
。
您的配置应该是这样的:
return array(
'controllers' => array(
'aliases' => array(
'Album\Controller\Album' => Controller\AlbumController::class
),
'factories' => array(
Controller\AlbumController::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'defaults' => array(
'controller' => Controller\AlbumController::class,
'action' => 'index',
),
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
由于我们已注册别名,您可以在路由默认值中替换控制器选项。因此,您可以使用'Album\Controller\Album'
而不是FQCN,但我更愿意在配置中使用FQCN。因为维护代码更容易。
您可能会遇到的下一件事是,您试图在控制器中使用getServiceLocator()
,但这不再受支持。查看:ZendFramework:ServiceManager-Factories