Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Slim 3:如何访问设置?_Php_Slim 3 - Fatal编程技术网

Php Slim 3:如何访问设置?

Php Slim 3:如何访问设置?,php,slim-3,Php,Slim 3,在Slim 3发布之前,以下代码正常工作: settings.php return [ 'settings' => [ 'displayErrorDetails' => true, 'modules' => [ 'core' => 'config/core/modules.php', 'local' => 'config/local/modules.php' ],

在Slim 3发布之前,以下代码正常工作:

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'];
    }
但发布后,我发现以下错误:

注意:未定义的属性:Slim\App::$settings in/


所以我不能再使用
$app->settings
?那么我应该使用什么呢?

您可以获得如下设置:

$container = $app->getContainer();
$settings = $container->get('settings');
var_dump($this->get('settings')['logger']);

您可以通过$this访问设置路由可调用项

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

有关更多信息,请参见SLIM 3配置文件的地址为pro/src/settings.php, 您还可以添加其他设置;在任何路线中,您都可以通过以下方式访问它们:

$container = $app->getContainer();
$settings = $container->get('settings');
var_dump($this->get('settings')['logger']);
我必须在这些行前面加上“global$app;”才能让它对我起作用。