2017-02-27 82 views
0

我想在路線確定後動態更新應用程序的設置值。例如,在大多數路由中,我將超時設置爲600秒。這已在設置文件中進行了硬編碼。Slim - 動態更改設置

return [ 
'settings' => [ 
    'timeout' => 600, 
....] 
]; 

但是在某些特殊路由中,我需要根據輸入將超時更改爲另一個值。例如

$app->get('/route1', function ($request, $response, $args) { 
    $timeout = (some calculations from $args) 
    $container['settings']['timeout'] = $timeout; 
}); 

但超時值設置爲容器,當我得到這個錯誤:

Notice: Indirect modification of overloaded element of Slim\Collection has no effect 

那麼什麼是動態更新設置的正確方法?

回答

1

雖然$container['settings']看起來像一個數組訪問,它實際上是一個閉合的是隨後返回數組這就是爲什麼你得到該通知的電話。解決方案是檢索數組,然後對其進行修改:

$settings = $container['settings'] 
$setting['timeout'] = $timeout; 
+1

This works。謝謝! – LazNiko