简体   繁体   中英

is it possible to use Zend Service Manager with custom codebase?

I would like to use Zend Service Manager in my custom codebase. It is not built on Zend MVC and instead I am only using a few Zend Packages like Zend Console and Zend Config.

I would like to use Zend Service Manager with a functionality, as soon as the code enters my Core class, I would like to initialize DB and Config objects and save them as a service in Zend Service Manager. Now I would like to use this service throughout my codebase.

Is there a way to use it without passing the service manager in constructor of each class? Also, my other classes will not be extending from this Core class and instead this Core class will create objects based on my requirements.

This is how I am creating a service:

    $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);

Yes, you can use it anywhere. This's the best part of zf, you can use it's parts anywhere. And no, you don't have to pass configuration on construct method. It has "setFactory" and "setService" methods and lots of others. checkout documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM