slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

Settings confusion #1478

Closed designermonkey closed 9 years ago

designermonkey commented 9 years ago

Looking at the Container code, I see the following

    public function __construct(array $values = [])
    {
        parent::__construct($values);

        $userSettings = isset($values['settings']) ? $values['settings'] : [];
        $this->registerDefaultServices($userSettings);
    }

I was under the impression that any settings we need to provide to Slim are just an array, not an array with a key of settings, which this container code is expecting?

In @akrabat's example repository here, settings is as I would expect, a simple array.

Shouldn't the $values not be passed through to Pimple, and the $userSettings be declared as such

$userSettings = isset($values['settings']) ? $values['settings'] : $values;

Otherwise, as I add keys to my settings array to match my services, like logger for example, when I come to declare my logger service, the key already exists in the container, and I will overwrite the settings with a service?

designermonkey commented 9 years ago

I can however see one benefit to this method, and that is that I can define my services within the $values array.

Hmmm, I can't decide which I prefer.

geggleto commented 9 years ago

The functionality was changed a few weeks ago to allow slim users to define container services at the root level.

I think you hinted on this... it is not possible to define container services \o/ during construction of the container

before the change

new Container(include "../containerConfig.php");

would place everything into a key 'settings' not allowing for MyClass::class resolution without marking the service ['setting'] in front of it... Making you either access the settings for your app's services (Silly) or define the services after container construction.

After the change you can define services in your config file like... You can even set your own settings by just placing them into the settings key

return [
   "settings" => [
        //... your settings
    ],
    MyClass::class => function ($c) {
        return new MyClass();
    }
];

Regarding your setting changes... you can see what would happen here. https://github.com/slimphp/Slim/blob/3.x/Slim/Container.php#L96-L98

        $this['settings'] = function ($c) use ($userSettings, $defaultSettings) {
            return array_merge($defaultSettings, $userSettings);
        };

I believe the precedence is right to left so your defaults override the slim defaults.

designermonkey commented 9 years ago

I'll leave this alone then, as I missed the change. It's fine to have it this way. Thanks for the info!