0

我想在我的自定义代码库中使用 Zend Service Manager。它不是基于 Zend MVC 构建的,而是我只使用一些 Zend 包,如 Zend Console 和 Zend Config。

我想使用 Zend Service Manager 的功能,只要代码进入我的 Core 类,我想初始化 DB 和 Config 对象并将它们作为服务保存在 Zend Service Manager 中。现在我想在我的代码库中使用这个服务。

有没有办法在每个类的构造函数中不传递服务管理器来使用它?此外,我的其他类不会从这个 Core 类扩展,而是这个 Core 类将根据我的要求创建对象。

这就是我创建服务的方式:

    $config = \Zend\Config\Factory::fromFiles([
        APPLICATION_PATH . '/config/config.ini',
        APPLICATION_PATH . '/config/' . $route->getMatchedParam('brand') . '/' . $route->getMatchedParam('department') . '/config.ini',
    ]);

    $serviceManager = new ServiceManager([
        'factories' => [
            stdClass::class => InvokableFactory::class,
        ],
    ]);

    $serviceManager->setService('config', $config);
4

1 回答 1

0

是的,您可以在任何地方使用它。这是 zf 最好的部分,您可以在任何地方使用它的部分。不,您不必在构造方法上传递配置。它具有“setFactory”和“setService”方法以及许多其他方法。结帐文件

于 2018-01-19T12:20:19.990 回答