0

I have read ZF documentation about ServiceManager and think configuration (even in "config" php files) like

public function getServiceConfig()
  {
    return array(
      'invokables' => array(
        'my-foo' => 'MyModule\Foo\Bar',
      ),
    );
  }

is very long and verbose. And, if I have a lot of dependencies, I want to use some sort of automatic code-geneation for this task.

In Symfony, I can to just write YAML configs like this:

parameters:
    mailer.transport: sendmail

services:
    mailer:
        class:     Mailer
        arguments: ["%mailer.transport%"]
    newsletter_manager:
        class:     NewsletterManager
        calls:
            - [setMailer, ["@mailer"]]

And it automatically compiles to PHP code by Symfony. Are there some solution to do similar work for ZF2? I don't think everybody writes tones of DI code instead of real work.

Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39

2 Answers2

1

You can wire up the Zend\Config\Reader\Yaml to parse your configs, but they aren't going to be any more or less verbose, just a different format. If you prefer that format, feel free, but PHP arrays are exceedingly flexible and useful for config like this.

Tim Klever
  • 601
  • 5
  • 13
  • That's certainly one way to do it. You could see if it's possible to wire it into the ConfigListener if it's going to be across the application. Up to you how to handle it really. However, for portability, I'd look long and hard at using raw php arrays – Tim Klever May 07 '15 at 20:40
  • Okay, but what about autogenerated getters for services and autocomplete in IDE? – Guy Fawkes May 08 '15 at 07:16
0

You generally will want to wire these in your module's configuration (e.g., module/Application/config/module.config.php).

The array syntax is shorter.

 return [
     'service_manager' => [
          'invokables' => [
               ...
           ],
      ],
 ];

Use ::class instead of class strings, it really cleans up the code, and makes it intuitive to invoke them with the SL throughout your app. Simply drop a 'use' statement at the top, and ::class away.

Don't sweat config if you are getting into ZF2. It's a pretty intuitive thing down the road, and though it may be a bit slower to wire components at first, once you get into it, you'll find ZF2 makes the very complex things easier than these other frameworks would; probably at the expense of making the easy things a bit more verbose.

ref: http://framework.zend.com/manual/current/en/modules/zend.service-manager.quick-start.html

Saeven
  • 2,280
  • 1
  • 20
  • 33