0

注册服务时module.config.php喜欢

'service_manager' => [
    'factories' => [
        \Path\To\Your\Service\AService => \Path\To\Your\Service\Factory\AServiceFactory,
    ]
]

在 ZF2(工厂实现时MutableCreationOptionsInterface)和 ZF3(通过$container->get(\Path\To\Your\Service\AService::class, $options).

谁能告诉我如何将创建选项传递给服务?

4

1 回答 1

1

MutableOptions目前仅在插件管理器实例上可用;服务管理器没有实现它。这就是您看到差异的原因。

参考:https ://github.com/zendframework/zend-servicemanager/issues/7

示例:https ://samsonasik.wordpress.com/2014/08/14/zend-framework-2-using-creationoptions-in-pluginmanager/

补充

我的解决方案是在 AService 类中添加一个具有流畅模式的方法:

class AService
{
    public function __construct(...)
    {
        //your code, you can inject variables from $container by AServiceFactory
    }
    public function setOptions($options)
    {
        // your setting from $options
        ...
        // fluent pattern
        return $this; 
    }
}

要使用您的服务:

$container->get(\Path\To\Your\Service\Aservice::class)->setOptions($options);
于 2016-11-20T17:54:01.760 回答