Php Zend\ServiceManager\ServiceManager::get无法获取或创建getAlbumTable的实例

Php Zend\ServiceManager\ServiceManager::get无法获取或创建getAlbumTable的实例,php,zend-framework,module,Php,Zend Framework,Module,我正在尝试修改和复制一个自定义模块,我已经设置了DB connection的所有内容,但在查看我的模块时出错,如下所示: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable 以下是my module.config文件: return array( 'controllers' => array( 'invokables' =>

我正在尝试修改和复制一个自定义模块,我已经设置了DB connection的所有内容,但在查看我的模块时出错,如下所示:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable
以下是my module.config文件:

return array(
 'controllers' => array(
     'invokables' => array(
         'Album\Controller\Album' => 'Album\Controller\AlbumController',
     ),
 ),


 // The following section is new and should be added to your file
 'router' => array(
     'routes' => array(
         'album' => array(
             'type'    => 'segment',
             'options' => array(
                 'route'    => '/album[/:action][/:id]',
                 'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     'id'     => '[0-9]+',
                 ),
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'index',
                 ),
             ),
         ),
     ),
 ),

 'view_manager' => array(
     'template_path_stack' => array(
         'album' => __DIR__ . '/../view',
     ),
 ),
);
下面是global.php中的数据库连接

return array(
'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=stickynotes;host=localhost',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),

'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter'
                => 'Zend\Db\Adapter\AdapterServiceFactory',
    ),
),
);
以下是module.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);
             },
         ),
     );
 }
   <?php 

   namespace Album\Controller;

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


   class AlbumController extends AbstractActionController
    {
     protected $_albumTable;

     public function indexAction()

      {
         return new ViewModel(array(

         'albums' => $this->getAlbumTable()->fetchAll(),

     ));         
      }

    }

    ?>
这是获取相册的控制器:

 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);
             },
         ),
     );
 }
   <?php 

   namespace Album\Controller;

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


   class AlbumController extends AbstractActionController
    {
     protected $_albumTable;

     public function indexAction()

      {
         return new ViewModel(array(

         'albums' => $this->getAlbumTable()->fetchAll(),

     ));         
      }

    }

    ?>

以下是数据库表的附件:


有人能告诉我在哪里调试这个错误并修复这个问题吗?

当您调用
$this->getAlbumTable()
Zend查找控制器插件时,没有这样名称的插件,所以它抛出异常

如果您想从
服务\u管理器
访问任何类,您必须通过工厂注入它

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use MyNamespace\Controller\AlbumController;

class AlbumControllerFactory implements FactoryInterface
{
    /**
     *
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param null|array $options
     * @return AlbumController
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $class = $requestedName ? $requestedName : AlbumController::class;
        $albumTable = $container->get('Album\Model\AlbumTable'); // get service from service manager
        $controller = new $class($albumTable);

        return $controller;

    }
    /**
     * Provided for backwards compatibility; proxies to __invoke().
     *
     * @param ContainerInterface|ServiceLocatorInterface $container
     * @return AlbumController
     */
    public function createService(ServiceLocatorInterface $container)
    {
        return $this($container, AlbumController::class);
    }
}

module.config.php中

'controllers' => array(
    'factories' => array(
        Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class,
    ),
),
在控制器中:

   namespace Album\Controller;

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


class AlbumController extends AbstractActionController
{

     protected $_albumTable;

     public function __construct(AlbumTable $albumTable)
     {
         $this->_albumTable = $albumTable;
     }

     public function indexAction()
     {
         return new ViewModel(array(
             'albums' => $this->_albumTable->fetchAll()

          ));         
     }
}

很简单,不是吗?:)

我刚刚通过在我的模块文件Module.php中添加服务解决了这个问题,现在it工作正常:

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);
             },
         ),
     );
 }

似乎我们需要的所有代码都不存在。表的服务管理器配置在哪里?如何将表注入控制器中?(这里有一个可调用的,而不是工厂,所以不能注入…)。您使用的是ZF3还是ZF2?它是ZF2。您希望看到代码的哪一部分,因此我将更新问题@Thomas Dutrion,该问题主要是配置文件中的任何
service\u manager
键,以及任何
->get(
代码中的任何位置(应主要在工厂中).显示调用
getAlbumTable的位置和方式
代码的第一部分将落在哪里,表示我的模块的哪个文件?@szymonm此行抛出错误,不让代码运行,$class=$requestedName?$requestedName:AlbumController::class;给出此错误:Controller:Album\Controller\Album(解析为无效的控制器类或别名:Album\controller\Album)在尝试创建controlleralbumcontroller(别名:controller\AlbumController)时,在
中的
module.config.php
中路由
更改
controller'=>“Album\Album
controller=>controller\AlbumController::class
)为此实例类型注册了无效的工厂。:此错误是什么@SzymonM