【问题标题】:php dont return the config file contentphp不返​​回配置文件内容
【发布时间】:2012-12-01 11:31:07
【问题描述】:
public function loadConfig($config)
{
    if(is_file(path('config') . $config . '.php'))
    {
        return include_once path('config') . $config . '.php';
    }

}

我在控制器中加载模型的功能相同,一切正常。但我不能包含配置文件,路径是正确的。如果我在return之前放

include_once path('config') . $config . '.php';
print_r($config_array);

它打印数组值

【问题讨论】:

  • include_once 在这里没有任何意义,您将希望在每次调用该方法时加载文件。另外,包含的文件是否包含return $config_array;
  • 我想在控制器中加载配置文件。是的,包含文件包含 $config_array。
  • 这个加载器必须类似于 Codeigniter 配置加载器

标签: php include filepath


【解决方案1】:

您将需要删除“_once”(因为在这种情况下防止第二次包含没有意义,它适用于类但不适用于配置文件)。此外,您需要在包含的文件中包含“return”语句或返回数组,而不是包含函数的返回值:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        include($filename);
        return $config_array;
    }

    // error handling, i.e., throw an exception ...
}

使用“return”语句的解决方案:

配置文件:

$config_array = array( ... );
return $config_array;

带有配置加载器方法的类:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        return include($filename);
    }

    // error handling, i.e., throw an exception ...
}

【讨论】:

  • 仍然有效,但如果我输入 echo 'include ?';在配置文件中,当打开控制器时,我看到了文本,但我无法从控制器访问配置数组。
猜你喜欢
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
相关资源
最近更新 更多