# Problem: DI Factories passing entire container

**URL:** https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083
**Category:** Questions
**Created:** [December 10, 2017, 11:53pm UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083 "2017-12-10T23:53:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![dstefani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dstefani/32/574_2.png) [@dstefani](https://discourse.slimframework.com/u/dstefani)
#### Post date: [December 10, 2017, 11:53pm UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/1 "2017-12-10T23:53:53Z")

</div>

Hi, I’m a bit new to frameworks and DI in general but I found this article helpful: [https://akrabat.com/di-factories-for-slim-controllers/](https://akrabat.com/di-factories-for-slim-controllers/)

But I’m having a problem, it seems that when I try to inject a dependency into a controller it’s getting passed the entire container object.

```
 $container['DatabaseService'] = function ($c) {
    return new \Lib\SqlModel(); // Aura/Sql wrapper
 };

$container['ServiceRecordsController'] = function($c) {
    $dbService = $c->get('DatabaseService');
    return new ServiceRecordsController($dbService);
};

```

My route looks like so:

```
$app->get('/records', ServiceRecordsController::class . ':show')
  ->setName('show_records');

```

And my controller looks like:

```
...
public function __construct($dbService)
  {
    $this->dbService = $dbService; // this is the container, not $c['DatabaseService']
    $this->serRecHandle = new ServiceRecords($this->dbService);
  }
...

```

when I eventually get to where I use $dbService I need to reference it as $dbService[‘DatabaseService’] and it works fine.

What am I missing?

Thanks - Don

---

<div class="post-metadata">

### Author: ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)
#### Post date: [December 11, 2017, 12:55am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/2 "2017-12-11T00:55:59Z")

</div>

If Slim doesn’t fid an entry for your controller in the container, it will pass the container to the controller’s constructor. So it would appear as though Slim isn’t finding the entry for your controller in the container if you are getting it in the constructor.

The issue isn’t obvious to me based on what you have posted. I wonder if it is perhaps a namespace issue, or perhaps needing a leading backlash.

```php
$app->get('/records', \ServiceRecordsController::class . ':show')

```

Is yoru controller class namespaced?

---

<div class="post-metadata">

### Author: ![dstefani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dstefani/32/574_2.png) [@dstefani](https://discourse.slimframework.com/u/dstefani)
#### Post date: [December 11, 2017, 3:08am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/3 "2017-12-11T03:08:01Z")

</div>

Yes it’s namespaced, I tried the leading slash and the app broke. Now that I know why it’s acting like it is I’ll work with it and see what I can find.

Thanks, Don

---

<div class="post-metadata">

### Author: ![nuwanda](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@nuwanda](https://discourse.slimframework.com/u/nuwanda)
#### Post date: [December 11, 2017, 6:28am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/4 "2017-12-11T06:28:36Z")

</div>

> [@dstefani](#):
>
> `return new \Lib\SqlModel();`

Check if that contains a reference to the container.

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [December 11, 2017, 10:27am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/5 "2017-12-11T10:27:55Z")

</div>

> [@dstefani](#):
>
> $container[‘ServiceRecordsController’] = function($c) {

Try to namespace the container ID e.g.

```php
$container[ServiceRecordsController::class] = function($c) {

```

---

<div class="post-metadata">

### Author: ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)
#### Post date: [December 11, 2017, 12:04pm UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/6 "2017-12-11T12:04:21Z")

</div>

If your class is namespaced, your container entry will likely need to be.

```php
$app->get('/records', App\Controllers\ServiceRecordsController::class . ':show')

```

---

<div class="post-metadata">

### Author: ![dstefani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dstefani/32/574_2.png) [@dstefani](https://discourse.slimframework.com/u/dstefani)
#### Post date: [December 11, 2017, 3:12pm UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/7 "2017-12-11T15:12:32Z")

</div>

Thanks, I’ll check this out tonight. This is on a personal project.

---

<div class="post-metadata">

### Author: ![ChaoticCoder](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@ChaoticCoder](https://discourse.slimframework.com/u/ChaoticCoder)
#### Post date: [May 4, 2019, 10:05am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/8 "2019-05-04T10:05:56Z")

</div>

Please excuse my bumping this old thread but, … to which result did you come @dstefani ?  
I’m asking this because I have exactly the same problem, and am still trying to find the solution.

---

<div class="post-metadata">

### Author: ![ChaoticCoder](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@ChaoticCoder](https://discourse.slimframework.com/u/ChaoticCoder)
#### Post date: [May 4, 2019, 10:18am UTC](https://discourse.slimframework.com/t/problem-di-factories-passing-entire-container/2083/9 "2019-05-04T10:18:05Z")

</div>

Oh my goodness,… literally 2 minutes after this post, I figured out the cause by having a look at an example API by @akrabat.  
The original code :

```auto
$container['FileServiceController'] = function ($container) {
        return new App\Controllers\FileService\FileServiceController($container['settings']['FileStoragePath']);
    };

```

The solution was to not add to container via string identifier, but to use the Class:class :

```auto
$container[App\Controllers\FileService\FileServiceController::class] = function ($container) {
        return new App\Controllers\FileService\FileServiceController($container['settings']['FileStoragePath']);
    };

```

Presumed you are using the Classname::class.’:method’ approach in your routes. It seems that whatever pattern you use in your routes, has to be used throughout the project, or more precisely the other way round.  
At least that does work for now, but probably introduces other issues.
