当前位置:首页 > PHP教程 > PHP高级教程

THINKPHP支持YAML配置文件的设置方法

为什么要用 yaml

因为 yaml 简单,而且对人类友好;

yaml: http://www.yaml.org/

在哪里会用到?

最基本的,在 thinkphp 的配置文件里面就可以选择 yaml 的格式,你是愿意写 yaml 的简洁格式配置呢?还是愿意写又长又臭又难写的 php array 呢?

http://document.thinkphp.cn/manual_3_2.html#config_format

问题来了

于是,我把配置格式改为 yaml:

复制代码 代码如下:

// /index.php

// 定义配置文件的格式为 yaml
define('conf_ext', '.yaml');

然后把 application 目录下面的 config.php 全部改为 config.yaml。

这个时候,报错:

复制代码 代码如下:

class ‘spyc' not found 错误位置 file: d:appthinkthinkphpcommonfunctions.php  line: 90

然后找,发现这一段:
复制代码 代码如下:

/**
 * 解析yaml文件返回一个数组
 * @param string $file 配置文件名
 * @return array
 */
if (!function_exists('yaml_parse_file')) {
    function yaml_parse_file($file) {
        vendor('spyc.spyc');
        return spyc::yamlload($file);
    }
}

貌似引入 spyc 这个库没找到。

找了一下,spyc 在这里:https://github.com/mustangostang/spyc/

然后,这个 vendor 是什么东东?

看这里:http://document.thinkphp.cn/manual_3_2.html#autoload

结论:

在上面的 git 里面下载 spyc.php 放进 /thinkphp/library/vendor/spyc/ 目录下,就一切正常!done with the right way!


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!