doumei2023 2014-12-07 19:33
浏览 232

在Laravel中加载json文件以进行数据库配置

I am recently working on a laravel project. This project needs a database: The data for its configuration is provided while installing this software and its saved in a sw-config.json located at in the root directory like the composer.json file ...

Now I want the laravel database.php to use the data from this file. I thought that I could simply let php load the json file via file_get_content() and set the data directly in app/config/database.php

But don't know why it doesn't work ... I have got troubles loading and decoding the json-file. Code looked like this.

$fokusConfigFile = file_get_contents(__DIR__."/../../content/sw-config.json");
$json = json_decode($fokusConfigFile, true);
$database = $json['database'];

$connection = array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $database['host'],
        'database'  => $database['database'],
        'username'  => $database['username'],
        'password'  => $database['password'],
        'charset'   => 'utf8',
        'collation' => $database['coalition'],
        'prefix'    => $database['prefix'],
    )
);

As an error message I receive Undefined index 'host' ... Don't see what could be wrong.

  • 写回答

1条回答 默认 最新

  • doutuo8800 2014-12-08 00:06
    关注

    Like @ceejayoz said: either var_dump() or print_r() the $database variable to inspect it. What I could think of is that the JSON returns an object and you thus need to access its values via

    $database->host
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大