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?
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.In
module.config.php
In your controller:
Simple, isn't it? :)
I have Just Solved it by adding the Services in my Module file Module.php now the it is working fine: