我想修改和複製一個自定義模塊我已經安裝了一切數據庫連接,但得到錯誤,同時要查看我的模塊,如下所示: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調試此錯誤並解決問題?
好像我們所需要的代碼是不存在的。這些表的service_manager配置在哪裏?你如何在控制器中注入你的表格? (這裏你有一個可調用的,而不是工廠,所以你不能注入...)。你使用ZF3還是ZF2? –
其zf2。你想看什麼代碼的一部分,所以我會更新問題@Thomas Dutrion – Shoaib
這將主要是你的配置文件中的任何'service_manager'鍵,以及任何' - > get('代碼中的任何地方(應該在工廠主要) –