# Solved: How to get access to settings in Action class

**URL:** <https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533>\
**Category:** Questions\
**Created:** [October 14, 2019, 2:19pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533 "2019-10-14T14:19:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiklop74](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kiklop74/32/987_2.png) [@kiklop74](https://discourse.slimframework.com/u/kiklop74)\
**Post date:** [October 14, 2019, 2:19pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/1 "2019-10-14T14:19:07Z")

</div>

Hi there,

I am new to slim framework and am currently building micro web service with latest version v4.3 and PHP 7.1.

My base is laying upon the [https://github.com/slimphp/Slim-Skeleton](https://github.com/slimphp/Slim-Skeleton) app.

I mostly figured out how this thing operates but am having an issue with accessing the container from the action class.

Base on the `routes.php` it seems that preffered way to set action classes is to place the actual class name

```
return function (App $app) {
    $app->get('/login/{t}', MyAction::class);
};

```

However if I place some custom parameters to the constructor I get class this and that can not be resolved.

So how does one handle this?

Or do I just instantiate the object with correct parameters and pass that as callable?

---

<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:** [October 14, 2019, 2:25pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/2 "2019-10-14T14:25:58Z")

</div>

The PSR-11 container implementation (PHP-DI) creates the objects for you.

[https://akrabat.com/dependency-injection-in-slim-4/](https://akrabat.com/dependency-injection-in-slim-4/)

---

<div class="post-metadata">

**Author:** ![kiklop74](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kiklop74/32/987_2.png) [@kiklop74](https://discourse.slimframework.com/u/kiklop74)\
**Post date:** [October 14, 2019, 2:37pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/3 "2019-10-14T14:37:41Z")

</div>

OK,  
In this specific case I did not need to specify anything in the container but it gave me the hint how to do it:

```
abstract class ApiAction extends Action
{
    /** @var array */
    protected $settings;

    /**
     * ApiAction constructor.
     * @param LoggerInterface $logger
     * @param ContainerInterface $c
     */
    public function __construct(LoggerInterface $logger, ContainerInterface $c)
    {
        parent::__construct($logger);
        $this->settings = $c->get('settings');
    }
}
```

---

<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:** [October 14, 2019, 5:49pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/4 "2019-10-14T17:49:19Z")

</div>

> [@kiklop74](#):
>
> ContainerInterface $c

Just I quick tip: The container should not passed to the action.

---

<div class="post-metadata">

**Author:** ![kiklop74](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kiklop74/32/987_2.png) [@kiklop74](https://discourse.slimframework.com/u/kiklop74)\
**Post date:** [October 14, 2019, 6:14pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/5 "2019-10-14T18:14:16Z")

</div>

So what should be done instead?  
I need those settings

---

<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:** [October 14, 2019, 6:52pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/6 "2019-10-14T18:52:04Z")

</div>

A “container friendly” solution would be to declare a special Configuration / Config class as dependency in your constructor.

```php
public function __construct(Config $config)
{
    $this->config = $config;
}

```

The Configuration object just needs to be defined and loaded like this:

```php
$containerBuilder->addDefinitions([
    Config::class => function () {
       return new Config([
           'displayErrorDetails' => true,
            //...
       ]);
    },
]);

```

**Edit** : There are other configuration classes available of course:

- [GitHub - laminas/laminas-config: provides a nested object property based user interface for accessing this configuration data within application code](https://github.com/laminas/laminas-config)
- [GitHub - hassankhan/config: Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files](https://github.com/hassankhan/config)
- [GitHub - codeliner/array-reader: PHP ArrayReader](https://github.com/codeliner/array-reader)

---

<div class="post-metadata">

**Author:** ![kiklop74](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kiklop74/32/987_2.png) [@kiklop74](https://discourse.slimframework.com/u/kiklop74)\
**Post date:** [October 15, 2019, 4:36pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/7 "2019-10-15T16:36:11Z")

</div>

Ok, this was helpful.

Thanks

---

<div class="post-metadata">

**Author:** ![Charlesfu4](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Charlesfu4](https://discourse.slimframework.com/u/Charlesfu4)\
**Post date:** [April 13, 2021, 8:20am UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/8 "2021-04-13T08:20:40Z")

</div>

Is this still the best practice in 2021? I went into your odan git repository and found the Configuration.php is already removed. Thank you!

---

<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:** [April 13, 2021, 6:26pm UTC](https://discourse.slimframework.com/t/solved-how-to-get-access-to-settings-in-action-class/3533/9 "2021-04-13T18:26:17Z")

</div>

When you declare an Config object the DI container can autowire this dependency. When you declare a config array you have to write a DI container definition (factory) manually for each class that requires some configuration key(s). On the other hand, objects that needs to be configured should be created within the DI container definition anyway. So in most cases an array is just “good enough”. From a higher level object-oriented code should use objects when possible. The downside of passing a Config object is that you may pass hundreds of other config keys and values that are never needed. The most libraries out here I know still use good old array for configuration.
