传递给Application \ Controller \ IndexController :: __ construct()的参数1必须是Application \ Model \ EmployeeTable的实例,没有给出

时间:2017-08-30 05:56:03

标签: zend-framework2

我坚持使用Zd2中的这个问题并且从最近2天敲了敲我的头但没有提出任何解决方案。将是非常值得注意的如果有人可以帮我解决下面给出的错误。 传递给Application \ Controller \ IndexController :: __ construct()的参数1必须是Application \ Model \ EmployeeTable的一个实例,没有给出

这是我的应用程序模块配置文件module.config.php

  

     

命名空间应用程序;

     

使用Zend \ Router \ Http \ Literal;使用Zend \ Router \ Http \ Segment;使用   的Zend \的ServiceManager \厂\ InvokableFactory;

     

返回[           [               'controllers'=> [                   'invokables'=> [                       'Application \ Controller \ Index'=> “应用程序\控制器\索引控制器”,                   ]                   '工厂'=> [                       'Application \ Controller \ Index'=> '应用程序\厂\ 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',
    ],
], ];

应用程序索引控制器

  

table = $ table;       }       public function indexAction(){           $ view = new ViewModel([               'data'=> $这 - >表 - >使用fetchall(),           ]);           return $ view;       }}

Module.php文件

  

     

class Module实现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)
                );
            },
        ],
    ];
} }

模型员工

  

  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

  

  使用Zend \ Db \ TableGateway \ TableGateway;   使用Zend \ Db \ TableGateway \ TableGatewayInterface;

     

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

请帮忙!

1 个答案:

答案 0 :(得分:0)

我上面的Module.php,要么写在类

之上
 use Model\EmployeeTable;

或者,将别名设置为"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
            )
        ];
    }