使用超薄的Container :: get('设置')与直接访问数组

时间:2018-02-21 16:49:41

标签: php slim

以下三种方法产生相同的效果。应该采用哪种方法?是否有时候应该使用另一个?

function getConfig() {
    $config=parse_ini_file(__DIR__.'/../config.ini',true);
    $config['directories']['base']=dirname(__DIR__);
    return $config;
}
$c = new \Slim\Container(['settings' => [
    'displayErrorDetails'=>true,
    'addContentLengthHeader'=>false,
    'determineRouteBeforeAppMiddleware'=>true,
    'config'=>getConfig()
]]);

$config=getConfig();
$c['pdo'] = function ($c) use($config){
    $db1 = $c['settings']['config']['mysql'];
    $db2 = $c->get('settings')['config']['mysql'];
    $db3 = $config['mysql'];
    return new \PDO(/* ... */);
};

旁注。是否建议对任何应用程序设置使用单独的设置属性,就像我使用" config"减少与Slim内部设置发生冲突的可能性(即httpVersionresponseChunkSizeoutputBufferingdetermineRouteBeforeAppMiddlewaredisplayErrorDetails,{{1} },addContentLengthHeader)?

1 个答案:

答案 0 :(得分:0)

获取所有设置只需调用get()方法,如下所示:

$settings = $container->get('settings');