# Containers: $app-\>getContainer() vs new Slim\\Container

**URL:** <https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094>\
**Category:** Questions\
**Created:** [January 23, 2017, 4:04pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094 "2017-01-23T16:04:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [January 23, 2017, 4:04pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094/1 "2017-01-23T16:04:46Z")

</div>

I was just curious if there were a difference or preference on how to instantiate containers in Slim between the two options:

```
$app = new Slim\App;
$di = new Slim\Container

```

vs.

```
$app = new Slim\App;
$di = $app->getContainer();

```

They both work fine. In my apps I’ve been using the first instance to create a bootstrap of dependency containers and on my routes I use the second instance to instantiate controllers for the route as noted below:

```
$app = new Slim\App;
$di = new Slim\Container;

$di['service'] = function() {
	return new App\Service;
};

// -------------------------------------
// some route

$c = $app->getContainer();

$c['Controller'] = function() use ($di) {
	return new App\Controller($di->service);
};

$app->get('/route','Controller:dashboard');

$app->run();
```

---

<div class="post-metadata">

**Author:** ![JustSteveKing](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@JustSteveKing](https://discourse.slimframework.com/u/JustSteveKing)\
**Post date:** [January 23, 2017, 4:13pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094/2 "2017-01-23T16:13:37Z")

</div>

I think this is so the container is injected correctly into the app, allowing you to `$app->container_name`

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [January 23, 2017, 4:35pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094/3 "2017-01-23T16:35:10Z")

</div>

@JustSteveKing Yep. I’ve gotten that part figured out. I’m just curious if there is a preference on how to instantiate the containers within the app or if it really doesn’t matter on how they’re set.

---

<div class="post-metadata">

**Author:** ![1ma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/1ma/32/314_2.png) [@1ma](https://discourse.slimframework.com/u/1ma)\
**Post date:** [January 23, 2017, 4:46pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094/4 "2017-01-23T16:46:28Z")

</div>

I prefer to setup the DI container and the Slim app in two separate steps.

```
<?php

// DI setup
$cnt = new Slim\Container;

$cnt['service'] = function() {
    return new App\Service;
};

$cnt['controller'] = function() use ($cnt) {
    return new App\Controller($cnt['service']);
};

// App setup
$app = new Slim\App($cnt);

$app->get('/route','Controller:dashboard');

$app->run();

```

Following this approach you can keep each step in a separate file, and have variations for different environments if you need them (e.g. di\_prod.php, app\_test.php)

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [January 23, 2017, 7:48pm UTC](https://discourse.slimframework.com/t/containers-app-getcontainer-vs-new-slim-container/1094/5 "2017-01-23T19:48:41Z")

</div>

@1ma I agree, although I use Controllers as an exception. Since I don’t share them I like to handle controllers on a ‘per route’ basis in their own container.
