2015-12-15 81 views
7

修身3之前被释放,下面做工精细代码:苗条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']; 
    } 

但发布后,我得到这个错误如下:

公告:未定义的属性:修身\ APP :: $在设置/ ...

所以我不能用$app->settings了吗?我应该使用什么?

回答

8

你可以像这样得到的设置:

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

我不得不包括“全球$ app;”在这些线路之前这为我工作。 –

5

您可以通过$本

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

访问设置路线可调用更多信息read here

1

的SLIM 3配置文件的地址是pro/src/settings.php, ,你可以添加额外的设置;在任何路线,你可以像这样访问他们:

var_dump($this->get('settings')['logger']);