鍍金池/ 問答/PHP/ ThinkPHP V5.1.18 跨模塊調(diào)用作用域問題

ThinkPHP V5.1.18 跨模塊調(diào)用作用域問題

1、分別有a模塊和b模塊,它們都有各自config配置文件

2、我在a模塊里調(diào)用b模塊控制器中的函數(shù),該函數(shù)卻使用了a模塊的config配置

3、我怎么才能讓它使用b模塊中的配置文件

回答
編輯回答
奧特蛋

這是tp5的機制,自動默認加載當前模塊下的配置文件。可以調(diào)用b模塊的函數(shù)前,先動態(tài)加載b模版的函數(shù)。

Config::load(APP_PATH.'b/config.php');

我看一下tp5.1的源碼, config的load類如下。

/**
 * 加載配置文件(多種格式)
 * @access public
 * @param  string    $file 配置文件名
 * @param  string    $name 一級配置名
 * @return mixed
 */
public function load($file, $name = '')
{
    if (is_file($file)) {
        $name = strtolower($name);
        $type = pathinfo($file, PATHINFO_EXTENSION);
        if ('php' == $type) {
            return $this->set(include $file, $name);
        } elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
            return $this->set(yaml_parse_file($file), $name);
        }
        return $this->parse($file, $type, $name);
    }
    return $this->config;
}

file的參數(shù)可以如下:

$obj->load('../application/b/config.php');

不嫌麻煩的,我建議定義一個常量APP_PATH

2018年3月19日 00:00