Zend Framework 2を使用して、モデルのファクトリ(TableGateway)をエレガントに作成する方法が必要です。
すべての依存性注入をファイルmodule.config.phpに入れたくありません。
Zend Framework 2を使用して、モデルのファクトリ(TableGateway)をエレガントに作成する方法が必要です。
すべての依存性注入をファイルmodule.config.phpに入れたくありません。
まあ、私もそれが好きではありません、そしてより良い読みやすさのために私はそれをすべてファクトリークラスに入れました。
Module.php
class Module implements \Zend\ModuleManager\Feature\ServiceProviderInterface
{
public function getServiceConfig()
{
return array(
'factories' => array(
'namespace-model-servicename' => 'Namespace\Factory\SomemodelServiceFactory'
)
);
}
}
Namespace \ Factory \ Servicename.php
<?php
namespace Namespace\Factory;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
use Namespace\Model\SomemodelService;
class SomemodelServiceFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return \Namespace\Service\SomemodelService
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$service = new SomemodelService();
$service->setEntityManager($serviceLocator->get('Doctrine\ORM\EntityManager'));
return $service;
}
}
これは、DoctrinesEntityManagerを注入するServiceFactoryの例です。明らかに、ニーズに合わせてすべてのクラスなどをリファクタリングする必要がありますが、基本的にはこれが実行するアプローチです。