How to get config in a Service

zend-mvc
zend-view

#1

Hi all,

I am not sure this is the right question or the right way of doing it.

I have a service called emailService, and i want to get the config from global.php. how to achieve that.

NOTE: i used factory to inject the config container but when I call this service in a controller I am getting errors, I think i need to pass some data to EmailService from controller as I am using constructor to get the config in EmailService.

if i am confusing please let me know…


#2

Like in your note, you should inject config as dependency of your service. What errors are you getting?


#3

for an instance.

Code below

my emailfactory

class EmailFactory

public function __invoke(ContainerInterface $container)
{
$config = $container->get(‘config’);

	$emailService =     $config['email'];

	return new IndexController($emailService);
}

emailservice

class EmailService

public function __construct($emailService)
{
$this->emailOptions = $emailService;
}

Controller

Class EmailController

public function sendemail()
{

$email = new EmailService();

// other email logic…
}

I am not sure is this right way of calling a Service in Controller…

do i need to assign something in EmailService( here? ) because EmailService is constructor …


#4

yeh, found the solution I totally understand how to use the factory and service, thanks for your reply cpm.


#5

Glad I could help. Generally you should consider creating EmailService that has injected configuration and inject that service into controller (or other place where you need it)


#6

Really appreciate your reply, yes I am doing the same…