# Can't set Slim's default settings after Slim\\Container instantiation

**URL:** <https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352>\
**Category:** Questions\
**Created:** [March 31, 2017, 2:01pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352 "2017-03-31T14:01:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eko3alpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/eko3alpha/32/350_2.png) [@eko3alpha](https://discourse.slimframework.com/u/eko3alpha)\
**Post date:** [March 31, 2017, 2:01pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/1 "2017-03-31T14:01:51Z")

</div>

I noticed a behavioral “quirk” ( not so much a bug ) with Slim’s [DI container](https://www.slimframework.com/docs/concepts/di.html) based on [Pimple](http://pimple.sensiolabs.org) implementation, and how it handles [Slim’s default settings](https://www.slimframework.com/docs/objects/application.html#slim-default-settings). I hope this helps other people since the documentation is a little light on this topic.

Originally I had the following array for the settings and all was good:

```
$c = new Container(['settings' => [
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
    'debug' => true]
]);

```

Inspecting the container and source code I noticed that “settings” key was special in that I didn’t need to list ALL the configuration options if passed in the Container constructor.

However as I tried to split up the settings and use the container to define the settings, I ran into the following issue.

**Won’t work**

public/index.php

```
$c = new Container;
.
.
require_once '../config/slim.php';
.
.
$app = new Slim($c);

```

config/slim.php

```
$c['settings'] = [
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
    'debug' => true]
];

```

This will trigger the following error:

```
 Undefined index: outputBuffering in /vendor/slim/slim/Slim/App.php on line 247

```

I can’t define application settings after I’ve instantiated the Container without having to define ALL the default configuration options. This is due to the “settings” key getting overridden and not merged, which makes 100% sense but…

**Will work**

index.php

```
// passing in settings in Container constructor
$c = new Container(['settings' => [
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
    'debug' => true]
]);

```

\*\* Alternatively I can just set EVERY config option \*\*

config/slim.php

```
// using container object
$c['settings'] = [
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
    'debug' => true,
    'httpVersion' => '1.1',
    'responseChunkSize' => 4096,
    'outputBuffering' => 'append',
    'addContentLengthHeader' => true,
    'routerCacheFile' => false
];

```

I was hoping to organize my config options and only use Slim’s container to keep things uniform. I understand why this behaves this way. If I want to use Slim’s container like my other configs I need to include all of [Slim’s default settings](https://www.slimframework.com/docs/objects/application.html#slim-default-settings). Since there are only a few it’s not a big deal, however this may become an issue if I want to set a custom logger or if Slim adds more config options in the future.

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [March 31, 2017, 8:37pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/2 "2017-03-31T20:37:46Z")

</div>

The Slim container constructor is indeed a bit quirky with regards to the settings entry. It [merges](https://github.com/slimphp/Slim/blob/3.x/Slim/Container.php#L96) the default settings with the settings entry passed in the constructor.

Maybe the following would work to update the settings?

```php
$c['settings'] = array_merge(
    $c['settings'], 
    [
        'displayErrorDetails' => true,
        'determineRouteBeforeAppMiddleware' => true,
        'debug' => true
    ]
);

```

Note: If you have a suggestion on how to improve the documentation, you can create an issue or a pull request at [https://github.com/slimphp/Slim-Website](https://github.com/slimphp/Slim-Website).

---

<div class="post-metadata">

**Author:** ![eko3alpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/eko3alpha/32/350_2.png) [@eko3alpha](https://discourse.slimframework.com/u/eko3alpha)\
**Post date:** [April 3, 2017, 1:24pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/3 "2017-04-03T13:24:24Z")

</div>

On the surface using array\_merge looks like it should work, except for this:

```
Warning: array_merge(): Argument #1 is not an array in /app/config/slim.php on line 5

```

and

```
Fatal error: Uncaught RuntimeException: Cannot override frozen service "settings". in /vendor/pimple/pimple/src/Pimple/Container.php on line 77

```

and

```
RuntimeException: Cannot override frozen service "settings". in /vendor/pimple/pimple/src/Pimple/Container.php on line 77

```

I’ve settled for passing an associative array in the Container constructor.

```
$c = new Container(['settings' => [
    'displayErrorDetails' => true,
    'determineRouteBeforeAppMiddleware' => true,
    'debug' => true]
]);
```

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [April 3, 2017, 6:14pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/4 "2017-04-03T18:14:29Z")

</div>

Sorry, I should have tried this with a small test application before posting.

The first error is because `$container['settings']` does not return an array as I thought, but a `Slim\Collection` instance.

The second and third error are probably because one the `$container['settings']` is called, the service is “frozen”. You can override the frozen service by calling `unset($container['settings'])`, but a better way is to use the `replace` method on the collection:

```php
$container
    ->get('settings')
    ->replace([
        'displayErrorDetails' => true,
        'determineRouteBeforeAppMiddleware' => true,
        'debug' => true
    ]);

```

The update function will replace values for existing keys and add any new values that are specified.

Hope that helps,

Lennaert

---

<div class="post-metadata">

**Author:** ![eko3alpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/eko3alpha/32/350_2.png) [@eko3alpha](https://discourse.slimframework.com/u/eko3alpha)\
**Post date:** [April 3, 2017, 6:54pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/5 "2017-04-03T18:54:09Z")

</div>

You’re right, it is returning a Collection instance, I didn’t bother to investigate further. While not ideal for my current structure this does work, and I’ll probably use this. Thanks!

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [April 3, 2017, 7:11pm UTC](https://discourse.slimframework.com/t/cant-set-slims-default-settings-after-slim-container-instantiation/1352/6 "2017-04-03T19:11:05Z")

</div>

No problem 🙂

I have created a pull request to add this information to the user guide.
