Slim 3: how to access settings?

别说谁变了你拦得住时间么 提交于 2019-12-10 02:21:00

问题


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?


回答1:


You can get settings like this:

$container = $app->getContainer();
$settings = $container->get('settings');



回答2:


You can access settings route callables via $this

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

For more information read here




回答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']);


来源:https://stackoverflow.com/questions/34281244/slim-3-how-to-access-settings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!