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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料