I have a service defined in Module.php
, where I inject my mail
config, defined in config/autoload/global.php
this way:
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
'Mailer' => function($container) {
return new MailService($this->getConfig()['mail']);
},
]
];
}
But I want to do it the ZF3 way (which I'm learning, so I defined my service in my module.config.php
this way:
return [
'services' => [
'factories' => [
Service\MailService::class => MailServiceFactory::class
]
],
And my MailServiceFactory.php
is:
class MailServiceFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new MailService();
}
}
But how can I retrieve my config defined in global.php
and inject it in the factory, needed by my service?
OK, after some debug and var_dump()
, I have it. I can access the config array thanks to $container->get('configuration')
. So my factory is now:
class MailServiceFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('configuration');
return new MailService($config['mail']);
}
}