Zend\ServiceManager\ServiceManager::get was unable

2019-07-05 03:04发布

I am trying to modify and copy a custom module i have setup everything DB connection is but getting the error while going to view my module as follows:

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

Here is the my module.config file:

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

And here is the database connection in 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',
    ),
),
);

Here is the code from the module.php for the services config:

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

Here is the controller to get the album:

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

     ));         
      }

    }

    ?>

Here is the attachment for the database tables: Database tables View

Can anyone please let me know where i have to debug this error and fix the problem?

2条回答
Rolldiameter
2楼-- · 2019-07-05 03:49

When you call $this->getAlbumTable() Zend looks for controller plugin, but there is none plugin with such name, so it throws exception.

If you want access any class from service_manager you have to inject it through factory.

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

In module.config.php

'controllers' => array(
    'factories' => array(
        Controller\AlbumController::class => Factory\Controller\AlbumControllerFactory::class,
    ),
),

In your controller:

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

          ));         
     }
}

Simple, isn't it? :)

查看更多
The star\"
3楼-- · 2019-07-05 03:54

I have Just Solved it by adding the Services in my Module file Module.php now the it is working fine:

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);
             },
         ),
     );
 }
查看更多
登录 后发表回答