zend-frameworkdependency-injectionzend-framework2servicemanager

Setting up ServiceManager with configuration files in Zend Framework 2


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.


Solution

  • 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.