Slim 3:如何访问设置?
Slim 3: how to access settings?
在 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'];
}
但是发布后,我得到以下错误:
Notice: Undefined property: Slim\App::$settings in /...
所以我不能再使用 $app->settings
了?那我该用什么?
您可以获得这样的设置:
$container = $app->getContainer();
$settings = $container->get('settings');
您可以通过 $this
访问设置路由调用
$modulesSettings = $this->get('settings')['modules']['local'];
更多信息read here
SLIM 3配置文件地址为pro/src/settings.php,
您可以添加其他设置;在任何路线中,您都可以像这样访问它们:
var_dump($this->get('settings')['logger']);
在 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'];
}
但是发布后,我得到以下错误:
Notice: Undefined property: Slim\App::$settings in /...
所以我不能再使用 $app->settings
了?那我该用什么?
您可以获得这样的设置:
$container = $app->getContainer();
$settings = $container->get('settings');
您可以通过 $this
访问设置路由调用$modulesSettings = $this->get('settings')['modules']['local'];
更多信息read here
SLIM 3配置文件地址为pro/src/settings.php, 您可以添加其他设置;在任何路线中,您都可以像这样访问它们:
var_dump($this->get('settings')['logger']);