1

I am very new in Zend Framework. Im following solid Zend Tutorial site (A to Z)..Im stuck here..please help me

some solution from SO didnt help much..doing exactly but nothing do..same post as below Zend Framework 2:Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable

Hitting this error

Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable, instance of Album\Model\AlbumTable given, called in C:\xampp\htdocs\zendF\module\Album\src\Module.php on line 43 

Stack Trace

#0 C:\xampp\htdocs\zendF\module\Album\src\Module.php(43): Album\Controller\AlbumController->__construct(Object(Album\Model\AlbumTable)) #1 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(758): Album\Module->Album\{closure}(Object(Zend\ServiceManager\ServiceManager), 'Album\\Controlle...', NULL) #2 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('Album\\Controlle...') #3 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php(141): Zend\ServiceManager\ServiceManager->get('Album\\Controlle...') #4 C:\xampp\htdocs\zendF\vendor\zendframework\zend-mvc\src\DispatchListener.php(95): Zend\ServiceManager\AbstractPluginManager->get('Album\\Controlle...') #5 C:\xampp\htdocs\zendF\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) #6 C:\xampp\htdocs\zendF\vendor\zendframework\zend-eventmanager\src\EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure)) #7 C:\xampp\htdocs\zendF\vendor\zendframework\zend-mvc\src\Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent)) #8 C:\xampp\htdocs\zendF\public\index.php(40): Zend\Mvc\Application->run() #9 {main} 

my Module.php

<?php namespace Album; use Zend\Db\Adapter\AdapterInterface; use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; use Zend\ModuleManager\Feature\ConfigProviderInterface; class Module implements ConfigProviderInterface { public function getConfig() { return include __DIR__ . '/../config/module.config.php'; } public function getServiceConfig() { return [ 'factories' => [ Model\AlbumTable::class => function($container) { $tableGateway = $container->get(Model\AlbumTableGateway::class); return new Model\AlbumTable($tableGateway); }, Model\AlbumTableGateway::class => function ($container) { $dbAdapter = $container->get(AdapterInterface::class); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Model\Album()); return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); }, ], ]; } public function getControllerConfig() { return [ 'factories' => [ Controller\AlbumController::class => function($container) { return new Controller\AlbumController( $container->get(Model\AlbumTable::class) ); }, ], ]; } } ?> 

my AlbumController.php

<?php namespace Album\Controller; use Zend\Mvc\Controller\AbstractActionController; class AlbumController extends AbstractActionController { private $table; public function __construct(AlbumTable $table) { $this->table = $table; } public function indexAction() { return new ViewModel([ 'albums' => $this->table->fetchAll(), ]); } public function addAction() { } public function editAction() { } public function deleteAction() { } } ?> 

my Module.config.php

<?php namespace Album; use Zend\Router\Http\Segment; return [ 'router' => [ 'routes' => [ 'album' => [ 'type' => Segment::class, 'options' => [ 'route' => '/album[/:action[/:id]]', 'constraints' => [ 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9]+', ], 'defaults' => [ 'controller' => Controller\AlbumController::class, 'action' => 'index', ], ], ], ], ], 'view_manager' => [ 'template_path_stack' => [ 'album' => __DIR__ . '/../view', ], ], ]; ?> 
4
  • your controller is confused to which dependency you are trying to inject in the constructor. To solve it you should set in 'use' declaration before your class like use Album\Model\AlbumTable; Commented Apr 15, 2019 at 4:00
  • ty very much @fmsthird for your feedback..i ady put use Album\Model\AlbumTable; inside AlbumController.php...but this time hit this error..Class 'Album\Controller\ViewModel' not found..sorry..im new to Zend Commented Apr 15, 2019 at 4:09
  • It's okay, I posted an answer you can check Commented Apr 15, 2019 at 4:13
  • As a heads-up, I think you might be doing a tutorial for ZF 2. ZF3 was released a few years ago, it's documentation and tutorials can be found here: docs.zendframework.com/tutorials. Injecting the ServiceManager into a controller is something that should no longer be done. Commented Apr 15, 2019 at 14:31

1 Answer 1

2

You should put the dependency after namespace.
Try this:

<?php namespace Album\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use Album\Model\AlbumTable; class AlbumController extends AbstractActionController { private $table; public function __construct(AlbumTable $table) { $this->table = $table; } public function indexAction() { return new ViewModel([ 'albums' => $this->table->fetchAll(), ]); } public function addAction() { } public function editAction() { } public function deleteAction() { } } 
Sign up to request clarification or add additional context in comments.

1 Comment

bro..works like charms!! some of mistake that i made 1. global.php 'dsn' => sprintf('sqlite:%s/data/zendF.db', realpath(getcwd())), (make sure db name is same) 2. adding use Album\Model\AlbumTable; inside controller

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.