2017-01-23 57 views
1

myconfig.php是否允许使用嵌套键访问Laravel配置值?

return [ 
    'key1' => [ 
     'nested_key1' => 1, 
     'nested_key2' => 2 
    ], 

    'key2' => [ 
     'nested_key1' => 1, 
     'nested_key2' => 2 
    ] 
]; 

所以我在我的config目录中的自定义配置。是否可以设置这样的值:config(['myconfig.key1.nested_key1' => 3])?我可以通过这种方式读取值,但似乎在设置新值时不起作用。

回答

2

是的,你可以做到这一点,它肯定工作:

config(['myconfig.key1.nested_key1' => 3]); 

echo config('myconfig.key1.nested_key1'); // Will output 3. 

值只会当前请求时被保存。如果你想保存下一个请求的数据,你应该使用像Laravel Config Writer或类似的包。

+0

所以要改变文件中的值我需要使用第三方解决方案? –

+0

@AndrewVershinin如果你想保存下一个请求的值,是的,你应该使用一个像['Laravel Config Writer'](https://github.com/daftspunk/laravel-config-writer) –

相关问题