# How to get config in a Service

**URL:** <https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196>\
**Category:** Components & MVC\
**Tags:** zend-mvc, zend-view\
**Created:** [July 12, 2017, 4:37pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196 "2017-07-12T16:37:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blackpearl](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blackpearl/32/103_2.png) [@Blackpearl](https://discourse.laminas.dev/u/Blackpearl)\
**Post date:** [July 12, 2017, 4:37pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/1 "2017-07-12T16:37:03Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![cpm](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/cpm/32/107_2.png) [@cpm](https://discourse.laminas.dev/u/cpm)\
**Post date:** [July 12, 2017, 4:53pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/2 "2017-07-12T16:53:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Blackpearl](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blackpearl/32/103_2.png) [@Blackpearl](https://discourse.laminas.dev/u/Blackpearl)\
**Post date:** [July 12, 2017, 7:08pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/3 "2017-07-12T19:08:37Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![Blackpearl](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blackpearl/32/103_2.png) [@Blackpearl](https://discourse.laminas.dev/u/Blackpearl)\
**Post date:** [July 13, 2017, 1:51pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/4 "2017-07-13T13:51:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpm](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/cpm/32/107_2.png) [@cpm](https://discourse.laminas.dev/u/cpm)\
**Post date:** [July 13, 2017, 8:38pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/5 "2017-07-13T20:38:26Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Blackpearl](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blackpearl/32/103_2.png) [@Blackpearl](https://discourse.laminas.dev/u/Blackpearl)\
**Post date:** [July 13, 2017, 9:42pm UTC](https://discourse.laminas.dev/t/how-to-get-config-in-a-service/196/6 "2017-07-13T21:42:27Z")

</div>

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