🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 新建core/Config.php ``` <?php namespace core; class Config { protected $config = []; // 扫描 config 文件夹,加入到配置的大数组 public function init() { foreach (glob(FRAME_BASE_PATH.'/config/*.php') as $file){ $key = str_replace('.php','',basename($file)); $this->config[$key] = require $file; } } // 获取配置 public function get($key) { $keys = explode('.',$key); $config = $this->config; foreach ($keys as $key) $config = $config[$key]; return $config; } // 重置配置的值 public function set($key, $val) { $keys = explode('.', $key); $newconfig = &$this->config; foreach($keys as $key) $newconfig = &$newconfig[$key]; // 传址 $newconfig = $val; } } ``` ### 绑定配置 `app.php` ![](https://img.kancloud.cn/1d/81/1d81688be60ad3995221e501e6998899_563x251.png) ### 加载配置 ![](https://img.kancloud.cn/01/56/01563a0340dd559b90d6d9103f74cd90_670x472.png) 现在 配置信息已经完成了,接下来是运行下看看。 ### 创建config/database.php ``` <?php return [ 'default' => 'hello world', 'connections' => [ 'mysql_one' => [ 'driver' => 'mysql', 'host' => '134.175.80.215', 'username' => 'php_frame', 'dbname' => 'php_frame', 'password' => '12345678', 'prefix' => '', 'options' => [ ] ], ] ]; ``` ### 编辑routes/web.php ``` $router->get('/config',function (){ echo App::getContainer()->get('config')->get('database.connections.mysql_one.driver').'<hr/>'; App::getContainer()->get('config')->set('database.connections.mysql_one.driver','mysql set'); echo App::getContainer()->get('config')->get('database.connections.mysql_one.driver'); }); ``` ![](https://img.kancloud.cn/d6/d8/d6d8e2a30a30887fc4263f21ab83b895_868x355.png) ![](https://img.kancloud.cn/2f/7f/2f7f1aa39e57dddf20739da5d0035134_509x232.png)