私はmodule.config.phpファイルに非共有サービスとコントローラファクトリを作成するファクトリを持っています。コンフィギュレーションのこの部分は次のようになります。ZendFramework2にServiceLocatorを注入しないで複数の非共有インスタンスを作成するには?
"service_manager" => [
"factories" => [
Entity\User::class => Factory\Entity\UserFactory::class
],
"shared" => [
Entity\User::class => false
]
],
"controllers" => [
"factories" => [
Controller\UserAdminController::class => Factory\Controller\UserAdminControllerFactory::class
]
]
私はので、私はUserエンティティクラスの複数のインスタンスをインスタンス化する必要があり、管理者は、同時に複数のユーザーを追加することができますので、ユーザエンティティは、非共有であります。
<?php
namespace User\Factory\Controller\Core;
use User\Controller\UserAdminController;
use Interop\Container\ContainerInterface;
use User\Entity\User;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class UserAdminControllerFactory implements FactoryInterface{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
*
* @return UserAdminController
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$serviceLocator = $container->getServiceLocator();
return new UserAdminController($serviceLocator->get(User::class));
}
/**
* @param ServiceLocatorInterface $serviceLocator
*
* @return UserAdminController
*/
public function createService(ServiceLocatorInterface $serviceLocator){
return $this($serviceLocator, UserAdminController::class);
}
}
現在のところ、それはコントローラのコンストラクタ内のUserエンティティクラスの単一インスタンスのみを注入:
は、ここに私のコントローラの工場です。しかし、管理者が何を望んでいるかに基づいて、未定義の新しいインスタンスが必要です。
可能な解決策の1つは、コントローラ内で$this->getServiceLocator()->get(User::class)
を呼び出すことによって、サービスロケータ全体を渡して新しいインスタンスを作成することですが、これはアンチパターンとみなされます。
ここでの質問は、UserFactoryクラスを使用して、コントローラでユーザーの新しいインスタンスを作成する方法です。
EDIT1:UserFactoryを追加します。
<?php
namespace User\Factory\Entity\Core;
use User\Entity\User;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class UserFactory implements FactoryInterface{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
*
* @return User
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$urlHelper = $container->get("ViewHelperManager")->get("url");
return new User($urlHelper);
}
/**
* @param ServiceLocatorInterface $serviceLocator
*
* @return User
*/
public function createService(ServiceLocatorInterface $serviceLocator){
return $this($serviceLocator, User::class);
}
}
ここにあなたが見ているものhttp://stackoverflow.com/questions/25447442/dependency-injection-in-factories?rq=1 私はよく分かりませんが、工場にコントローラを挿入してユーザーエンティティ – bxN5
UserFactoryのコンテンツも共有できますか?ドメインエンティティ用のファクトリを作成する強い理由はありますか?とにかく、私はそのようなタスクのコントローラプラグインを作成しようとします。 – edigu
@Roman私は私の工場でサービスロケータが必要なので、私は工場を注入する場合、私は同様にアンチパターンであるサービスロケータを注入する必要があります。したがって、私はこれを行うことはできません。何か案は? – thexpand