2017-03-07 42 views
1

我想修改和複製一個自定義模塊我已經安裝了一切數據庫連接,但得到錯誤,同時要查看我的模塊,如下所示:Zend ServiceManager ServiceManager :: get無法獲取或創建一個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

任何人都可以請讓我知道我在哪裏hav e調試此錯誤並解決問題?

+0

好像我們所需要的代碼是不存在的。這些表的service_manager配置在哪裏?你如何在控制器中注入你的表格? (這裏你有一個可調用的,而不是工廠,所以你不能注入...)。你使用ZF3還是ZF2? –

+0

其zf2。你想看什麼代碼的一部分,所以我會更新問題@Thomas Dutrion – Shoaib

+0

這將主要是你的配置文件中的任何'service_manager'鍵,以及任何' - > get('代碼中的任何地方(應該在工廠主要) –

回答

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

     ));   
    } 
} 

簡單,不是嗎? :)

+0

哪裏代碼的第一部分將落下,意味着我的模塊的哪個文件?@SzymonM – Shoaib

+0

此行拋出一個錯誤,不要讓代碼運行?, $類= $ requestedName $ requestedName:AlbumController ::類; – Shoaib

+0

給我這個錯誤: 控制器: 專輯\控制器\相冊(解析爲無效的控制器類或別名:專輯\控制器\專輯) – Shoaib

相關問題