2015-12-15 22 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']);