依存性注入を実装しようとしています。Zend2サービスマネージャを使用しています。私はサービスにPDOインスタンスを挿入したい(Zend Dbを使用しない)。ここではチュートリアル以下Zend2サービスへの依存性注入工場
イム:https://framework.zend.com/manual/2.4/en/in-depth-guide/services-and-servicemanager.html
私はそれが他のサービスのために働いてきたが、PDOインスタンスを注入する際イムは、このエラーを取得:
Catchable fatal error: Argument 1 passed to Application\Service\DataService::__construct() must be an instance of Application\Service\DbConnectorService, none given, called in /srv/www/shared-apps/approot/apps-dev/ktrist/SBSDash/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 1077 and defined in /srv/www/shared-apps/approot/apps-dev/ktrist/SBSDash/module/Application/src/Application/Service/DataService.php on line 24
チュートリアルから、これが関係しているように見えますmodule.config内の私の呼び出し可能ファイルに追加します。しかし、私は問題が何かを解決することはできません。
アドバイスありがとうございます。ここで
私のコードです:
のDataService:
class DataService {
protected $dbConnectorService;
public function __construct(DbConnectorService $dbConnectorService) {
$this->dbConnectorService = $dbConnectorService;
}
......
DataServiceFactory:
namespace Application\Factory;
use Application\Service\DataService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class DataServiceFactory implements FactoryInterface {
function createService(ServiceLocatorInterface $serviceLocator) {
$realServiceLocator = $serviceLocator->getServiceLocator();
$dbService = $realServiceLocator->get('Application\Service\DbConnectorService');
return new DataService($dbService);
}
}
Module.Config:
'controllers' => array(
'factories' => array(
'Application\Controller\Index' => 'Application\Factory\IndexControllerFactory',
'Application\Service\DataService' => 'Application\Factory\DataServiceFactory',
)
),
'service_manager' => array(
'invokables' => array(
'Application\Service\DataServiceInterface' => 'Application\Service\DataService',
'Application\Service\DbConnectorService' => 'Application\Service\DbConnectorService',
)
),
'factory'は' controller'配列にありますか?それは 'service_manager'にあるはずです。 – akond