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

时间:2017-03-07 08:31:09

标签: php zend-framework module

我正在尝试修改和复制自定义模块我已经设置了数据库连接的所有内容,但在查看模块时遇到错误,如下所示:

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

任何人都可以告诉我在哪里调试此错误并解决问题?

2 个答案:

答案 0 :(得分:0)

当你调用$this->getAlbumTable()时,Zend会查找控制器插件,但是没有带有这种名称的插件,所以它会抛出异常。

如果你想访问service_manager中的任何一个类,你必须通过工厂注入它。

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()

          ));         
     }
}

简单,不是吗? :)

答案 1 :(得分:0)

我刚刚通过在我的模块文件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);
             },
         ),
     );
 }