“无法解析服务”Album \ Controller \ AlbumController“到工厂; 你确定你在配置期间提供了吗?“在Zend Framework 2中
I am getting an error in Zend Framework 2 tutorial project as,
Unable to resolve service "Album\Controller\AlbumController" to a factory; are you certain you provided it during configuration?
My Module.php as
namespace Album;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\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)
);
},
],
];
}
}
And my module.config.php is;
namespace Album;
use Zend\Router\Http\Segment;
//use Zend\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
'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',
],
],
];
I am very new in Zend Framework. Just can not figure out what I am doing wrong. Please mention if some more code is necessary.
You have defined two factories for "Controller\AlbumController::class"...
One in module.config.php -
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
],
and the second one in Module.php-
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
They are both conflicting with each other. Invokable factories are for classes that doesn't have have any dependencies. In this case it looks like "AlbumController" has a dependency of "AlbumTable", so the second option looks like the one you want.
So in short, remove the 'controllers' key value from the array in module.config.php.
Hope this helps!
I had the same problem, and a few other related problems - until I finally realized the real source of the issues: I was following the tutorial for Zend 2, while what I had installed is Zend 3!
If you have Zend 3, by all means use the tutorial for Zend 3, here: https://docs.zendframework.com/tutorials/getting-started/overview/