0

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);
Raja Amer Khan
  • 1,489
  • 10
  • 31

1 Answers1

0

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

  • Thanks for the reply, but I want to make this service available throughout my codebase. I am using composer PSR-4 autoloader path to load my project. So the solution I came up with is to create a new class with Static method and return Zend Service Manager from it, this way I can call it's object throughout my code. Isn't there a direct method of keeping this service available throughout my code? – Raja Amer Khan Jan 19 '18 at 14:03
  • Well.. static method is not that bad. Yes it's violating dependency injection. But if you have to use it, i think it's ok. I don't know your codebase.How's your algorithm works? Is it running on DI or global variables? If you are using global variables, define a variable on "init.php" page, and use it. If we talk about zend-mvc, ApplicationFactory build's application, and index.php triggers "run" method of it. After that, when you need service-manager, application gives you same instance. If you can write this with static method, sure use it. – Mehmet SÖĞÜNMEZ Jan 20 '18 at 14:32
  • Also you can check zf-expressive's init page. It's builds service manager first, then runs application. This can be an other approach. – Mehmet SÖĞÜNMEZ Jan 20 '18 at 14:33
  • Actually my application is only for cron jobs but I am bound to use Zend for it. Therefore I started off with this: https://github.com/zfcampus/zf-console now with Console based application, I don't have any MVC or service based structure. I have kept my code in a Src folder and loaded it through Composer. Now I want to achieve this service functionality so I can keep all my objects like; Log, Config, DB1, DB2 in it so that I can quickly access it throughout my codebase. Is it ok to skip this Zend service and instead create a class with static function to get and set the objects? – Raja Amer Khan Jan 20 '18 at 15:17
  • The flow will be: My cron job will have different options like Limit, Stage etc.. based on these options I will be calling a class object, and that object will then again use multiple other objects, therefore, instead of injecting all dependencies I want to make my main objects available throughout. So what do you suggest for it? – Raja Amer Khan Jan 20 '18 at 15:21
  • 1
    Oh there is an example for this in readme file for router . Same aproach can be use for dispatcher interface with manuel mapping to factory files. I Will write an example gist tomorrow. Limited internet access atm, sorry. – Mehmet SÖĞÜNMEZ Jan 20 '18 at 20:13
  • Please do, will really help. Thanks – Raja Amer Khan Jan 20 '18 at 22:30
  • I'm so sorry about late. I just accessed proper internet connection. Your example will be in github in 1-2 hours. – Mehmet SÖĞÜNMEZ Jan 26 '18 at 14:22
  • Hi, thanks for getting back to me. It took a little time therefore I got rid of zend service manager altogether and am using just the singleton classes for now. If you have created the code anyway, then please do share. It might help others. Thanks – Raja Amer Khan Jan 26 '18 at 14:56