我有以下控制器:Zend的2注入到控制器:開捕致命錯誤:傳遞給someController參數1 :: __構造()必須是...沒有一個實例給出
namespace Application\Controller;
use Application\Model\Person;
use Zend\Mvc\Controller\AbstractActionController;
use Application\Model\PersonTable;
class PersonController extends AbstractActionController
{
private $table;
public function __construct(PersonTable $table)
{
$this->table = $table;
}
// other methods
}
我試着做注射通過這裏下列文件:
https://docs.zendframework.com/tutorials/getting-started/database-and-models/
在模塊/應用程序/ Module.php添加此功能:
public function getControllerConfig()
{
return [
'factories' => [
Controller\PersonController::class => function($container) {
return new Controller\PersonController(
$container->get(Model\PersonTable::class)
);
},
],
];
}
在模塊/應用/配置/ module.config.php我修改了這一點,所以它有我的控制器:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Person' => 'Application\Controller\PersonController'
),
),
我打電話從控制檯的控制方法,並得到了錯誤:
可捕獲致命錯誤:將參數1傳遞給Application \ Controller \ PersonControl ler :: __ construct()必須是Application \ Controller \ PersonTable的一個實例,否則爲 ne,在E中調用:\ other \ dropbox \ Dropbox \ programavimas \ kodo pavyzdziai \ htdoc s \ zend_2_staff_register \ vendor \ zendframework \ zendframework \ library \ Zend \ ServiceM anager \ AbstractPluginManager.php on line 170 and defined in E:\ other \ dropbox \ Dro pbox \ programavimas \ kodo pavyzdziai \ htdocs \ zend_2_staff_register \ module \ Applicati on \ src \ Application \ Controller \ PersonController.php on line 12
爲什麼不注射?