How to get config in a Service

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…

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

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 …

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

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)

1 Like

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