Slim 3: how to access settings?
Asked Answered
S

3

14

Before the Slim 3 is released, codes below work fine:

settings.php,

return [
    'settings' => [
        'displayErrorDetails' => true,
        'modules' => [
           'core' => 'config/core/modules.php',
           'local' => 'config/local/modules.php'
        ],
    ],
];

index.php

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

$MyClass = new MyClass($app);

MyClass.php

class MyClass
{
    private $app;

    public function __construct($app)
    {
        $this->app = $app;
        $local = require $app->settings['modules']['local'];
    }

But after the release, I get this error below:

Notice: Undefined property: Slim\App::$settings in /...

So I can't use $app->settings anymore? What should I use then?

Soloman answered 15/12, 2015 at 4:39 Comment(0)
R
17

You can get settings like this:

$container = $app->getContainer();
$settings = $container->get('settings');
Rudy answered 15/12, 2015 at 10:29 Comment(1)
I had to include "global $app;" before these lines for this to work for me.Hygrostat
B
7

You can access settings route callables via $this

$modulesSettings = $this->get('settings')['modules']['local'];

For more information read here

Brundage answered 21/7, 2016 at 10:57 Comment(0)
H
3

The address of the SLIM 3 configuration file is pro/src/settings.php, and you can add additional settings; In any route you can access them like this:

var_dump($this->get('settings')['logger']);
Hornbeck answered 28/6, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.