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

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable

我正在尝试修改和复制一个自定义模块,我已经设置了所有数据库连接,但是在按如下方式查看我的模块时出现错误:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable

这是我的 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(),

     ));         
      }

    }

    ?>

这是数据库表的附件: Database tables View

任何人都可以让我知道我必须在哪里调试这个错误并解决问题吗?

当您调用 $this->getAlbumTable() Zend 寻找控制器插件时,但是有 none 插件具有这样的名称,因此它会抛出异常。

如果你想从 service_manager 访问任何 class,你必须通过工厂注入它。

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 现在它工作正常:

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