Mukesh Sharma
Mukesh Sharma

Reputation: 1

Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Application\Model\EmployeeTable, none given

I stuck With this Issue in Zd2 and knocking my head from last 2 days but not come up with any solution. Will be very Appreciable If anyone Can help me out with the below given error. Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Application\Model\EmployeeTable, none given

Here is my Application Module Config file module.config.php

namespace Application;

use Zend\Router\Http\Literal; use Zend\Router\Http\Segment; use Zend\ServiceManager\Factory\InvokableFactory;

return [ [ 'controllers' => [ 'invokables' => [ 'Application\Controller\Index' => 'Application\Controller\IndexController', ], 'factories' => [ 'Application\Controller\Index' => 'Application\Factory\IndexControllerFactory' ], ]

    ],
    'router' => [
    'routes' => [
        'home' => [
            'type' => Literal::class,
            'options' => [
                'route'    => '/',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
        ],
        'application' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/application[/:action]',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
        ],
    ],
],
'view_manager' => [
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => [
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
], ];

Application Index Controller

table = $table; } public function indexAction() { $view = new ViewModel([ 'data' => $this->table->fetchAll(), ]); return $view; } }

Module.php file

class Module implements ConfigProviderInterface { const VERSION = '3.0.3-dev';

public function getConfig()
{
    return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig() {
    return  [
        'factories' => [
            Model\EmployeeTable::class => function ($container) {
                $tableGateway = $container>get(Model\EmployeeTableGateway::class);
                $table = new Model\EmployeeTable($tableGateway);
                return $table;
            },
            Model\EmployeeTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Employee());
                return new TableGateway('employee', $dbAdapter, null, $resultSetPrototype);
            },
        ],
    ];
}
public function getControllerConfig() {
    return  [
        'factories' => [
            Controller\IndexController::class => function($container) {
                return new Controller\IndexController(
                    $container->get(EmployeeTable::class)
                );
            },
        ],
    ];
} }

Model Employee

class Employee { public $id; public $emp_name; public $emp_job; public function exchangeArray($data) { $this->id = (!empty($data['id'])) ? $data['id'] : null; $this->emp_name = (!empty($data['emp_name'])) ? $data['emp_name'] : null; $this->emp_job = (!empty($data['emp_job'])) ? $data['emp_job'] : null; } }

Model EmployeeTable

use Zend\Db\TableGateway\TableGateway; use Zend\Db\TableGateway\TableGatewayInterface;

class EmployeeTable { protected $tableGateway; public function __construct(TableGatewayInterface $tableGateway) { $this->tableGateway = $tableGateway; } public function fetchAll() { $resultSet = $this->tableGateway->select(); return $resultSet; } }

Please help !

Upvotes: 0

Views: 450

Answers (1)

Gautam Rai
Gautam Rai

Reputation: 2505

I the above Module.php, either write on top of the class

 use Model\EmployeeTable;

Or, set an aliases to "Model\EmployeeTable::class"

public function getServiceConfig() {
        return  [
            'factories' => [
                Model\EmployeeTable::class => function ($container) {
                    $tableGateway = $container>get(Model\EmployeeTableGateway::class);
                    $table = new Model\EmployeeTable($tableGateway);
                    return $table;
                },
                Model\EmployeeTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Employee());
                    return new TableGateway('employee', $dbAdapter, null, $resultSetPrototype);
                },
            ],
            'aliases' => array(
                EmployeeTable::class => Model\EmployeeTable::class
            )
        ];
    }

Upvotes: 1

Related Questions