I noticed a behavioral “quirk” ( not so much a bug ) with Slim’s DI container based on Pimple implementation, and how it handles Slim’s 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. 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.