简体   繁体   中英

Setting/retrieving mode in Slim Framework V3

I am new to Slim Framework (PHP). I was going through tutorials and testing some code and tried the following:

$app=new App([
    'mode'=>file_get_contents(INC_ROOT . '/mode.php')
]);
echo $app->config('mode');

The output was blank.

It seems the function config() works with V2, however I am using V3. What am I doing wrong?

Slim 3 no longer has a config method. Instead, you must add configuration settings through the dependency injection container :

$app = new \Slim\App([
    'settings' => [
        'mode' => true
    ]
]);

$container = $app->getContainer();
echo $container->get('settings')['mode'];

A few things worth noting:

Slim 3 no longer handles managing different versions of configuration settings through a mode setting. So, you can set a value for a setting variable called mode as I've demonstrated here, but it won't actually do anything (ie, Slim won't use it to determine your environment).

As an alternative, you can check out userfrosting/Config , a library we've been working on that can search multiple directories and different environment configuration files, merging together their contents:

/path/to/config/default.php

return [
    'contacts' => [
        'housekeeper' => [
            'name' => 'Alex',
            'email' => 'alex@cleansthetoilet.com'
        ]
    ]
];

/path/to/config/production.php

return [
    'contacts' => [
        'housekeeper' => [
            'email' => 'alex@istheboss.com'
        ]
    ],
    'database' => [
        'password' => 'sup3rC-cr3t'
    ]
];

index.php

$app = new \Slim\App();

$container = $app->getContainer();    

// Site config object (separate from Slim settings)
$container['config'] = function ($c) {
    // Create and inject new config item
    $config = new \UserFrosting\Config\Config();

    $config->setPaths([
        '/path/to/config'
    ]);

    $config->loadConfigurationFiles('production');

    return $config;
};

This will recursively merge in the settings from development.php with those in default.php , updating settings with the same name and scope as necessary:

Running print_r($container['config']); returns:

[
    'contacts' => [
        'housekeeper' => [
            'name' => 'Alex',
            'email' => 'alex@istheboss.com'
        ]
    ],
    'database' => [
        'password' => 'sup3rC-cr3t'
    ]
]

Notice that the value of contacts.housekeeper.email has been updated to 'alex@istheboss.com' , and that the database config info has been merged in. Incidentally, you can also access config settings using the more convenient "dot syntax":

$config = $container->get('config');
echo $config['contacts.housekeeper.email'];
// Easier to type instead of $config['contacts']['housekeeper']['email'];

We recommend injecting this as a separate config service in Slim, rather than using their settings array.

You can combine this with phpdotenv to load settings from your system environment, or any .env files you create:

/path/to/config/production.php

return [
    'database' => [
        'password' => getenv('DB_PASSWORD')
    ]
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM