I'm currently writing my own PHP Framework (for kicks, not for mission critical stuff) and I'm trying to add in functionality where the user can set up what databases the framework should use (a primary db and then maybe one or two fallbacks - like sqlite), where certain files are located, etc. Should I use YAML for this? Is there a better approach or a standard practice?
我正在编写自己的PHP框架(用于踢,而不是用于关键任务的东西),我正在尝试添加功能,用户可以设置框架应该使用的数据库(一个主数据库然后可能是一个或两个)回退 - 比如sqlite),某些文件所在的位置等等。我应该使用YAML吗?有更好的方法或标准做法吗?
My Thoughts
我的想法
- YAML is user (non-technical) friendly in terms of readability
- YAML在可读性方面是用户(非技术)友好的
- In order to keep my Framework from requiring non-standard PHP libraries, I'd have to use something like Symfony YAML in order to parse the file.
- 为了防止我的Framework需要非标准的PHP库,我必须使用像Symfony YAML这样的东西来解析文件。
- Isn't Symfony moving away from YAML?
- Symfony不是要离开YAML吗?
- I could use a PHP file full of variables but that would make setup of the framework less transparent to the user.
- 我可以使用一个充满变量的PHP文件,但这会使框架的设置对用户不太透明。
Update
更新
I'm cleaning this question up to make it more constructive and to incorporate some of the answers I've gotten.
我正在清理这个问题,以使其更具建设性,并纳入我得到的一些答案。
Overall Questions I Have
我有的总体问题
- What are the advantages of YAML over other approaches like XML or even an INI file setup?
- 与其他方法(如XML甚至INI文件设置)相比,YAML有哪些优势?
- What is a good rule of thumb regarding when to use YAML over those other approaches or vice versa?
- 关于何时使用YAML而不是其他方法,反之亦然,有什么好的经验法则?
6 个解决方案
#1
25
NO
Personal experience. YAML seems a wonderful idea and I loved it and its simplicity. Then I started investing time on it: the very same concept of being able to read it in a language and write in another was very enticing, but... cutting it short, it turned up to be a mere illusion, unsubstantiated by facts.
个人经验。 YAML似乎是一个很棒的主意,我很喜欢它和简单。然后我开始把时间花在它上面:能够用一种语言阅读它并在另一种语言中写入的相同概念非常诱人,但是......简而言之,它变成了一种纯粹的幻觉,没有事实根据事实证明。
Every implementation of YAML differs too much from the other ones.
YAML的每个实现都与其他实现有很大不同。
- Arrays, automatically serialized by one, cannot sometimes be read by another.
- 由一个自动序列化的数组有时不能被另一个读取。
-
Cross references are supported, but their implementation is really sketchy.
支持交叉引用,但它们的实现非常粗略。
References are powerful, but:
参考文献很有用,但是:
- They are quite limited for some hardcore application.
- 对于某些硬核应用,它们非常有限。
- They represent an overkill to most low-end YAML-based projects.
- 它们代表了对大多数基于YAML的低端项目的过度杀伤力。
So, frequently, they are ignored, and errored upon, by most parsers.因此,大多数解析器经常会忽略它们并导致错误。
Summing it up, the standard isn't well set.
总结一下,标准设置不好。
There are core concepts that are nice and simple, but the actual standard document is full of details about features that most people don't want to use and are difficult and expensive to implement.
有一些核心概念很简单,但实际的标准文档中充满了大多数人不想使用的功能的详细信息,并且实现起来既困难又昂贵。
There isn't a distinction of levels of compatibility, like there is in DOM (DOM level 1, DOM level 2, etc) so every parser implementor implements what he feels like to, to the extent he can bear, then drops it and it's hard to discern what works and what doesn't.
没有区分兼容性级别,例如DOM(DOM级别1,DOM级别2等),因此每个解析器实现者都实现了他的感觉,在他能够承受的范围内,然后丢弃它并且它是很难辨别哪些有效,哪些无效。
Use Alternatives
-
JSON if you value the cross language data exchange language and little redundancy aspects as the top priority
如果您重视跨语言数据交换语言和少量冗余方面作为最高优先级,则为JSON
-
INI if you value performance and backward compatibility (on PHP, as
parse_ini_file()
is fast, and there since... always) and readability/editability by humans, instead.INI,如果你重视性能和向后兼容性(在PHP上,因为parse_ini_file()是快速的,并且因为......总是)和人类的可读性/可编辑性。
#2
4
My personal preference is a for a PHP based configuration file.
我个人的偏好是基于PHP的配置文件。
i know php so to me learning yaml just for the config files is extra work when you could have a simple config file like this, which in essence is no harder them yaml, and doesn't require a special interpreter library, just include('config.php') and you are away
我知道php所以对我来说学习yaml只是为了配置文件是额外的工作,当你可以有这样一个简单的配置文件,这本质上是没有更难他们yaml,并不需要一个特殊的解释器库,只是包括(' config.php')你离开了
$config = array(
'database' => array(
'default' => array(
'name' => 'dbname',
'host' => 'localhost',
'user' => 'username',
'pass' => 'password'
)
)
);
then you can reference config settings like this
然后你可以像这样引用配置设置
$host = $config['database']['default']['host'];
next step is to keep the config file simple, store the minimal amount of config data required, and then use the database to store the rest, and have admin screens for endusers to change settings within your application.
下一步是保持配置文件简单,存储所需的最少量配置数据,然后使用数据库存储其余数据,并为最终用户提供管理屏幕以更改应用程序中的设置。
#3
1
If you're writing a framework, then yes. You will have to end up doing more work on your part, but the goal of a framework is to make things easier for the person developing the application.
如果您正在编写框架,那么是的。您将不得不最终完成更多工作,但框架的目标是使开发应用程序的人员更容易。
Isn't Symfony moving away from YAML?
Symfony不是要离开YAML吗?
No, Symony2 is almost entirely configured by YAML.
不,Symony2几乎完全由YAML配置。
#4
1
I have dig bit more for config file format and found interesting facts for YAML format.
我为配置文件格式挖掘了更多,并为YAML格式找到了有趣的事实。
Mainly it have few drawback
主要是它有一些缺点
1) It involve installation and configuration of addition PHP library as YAML Module is not coming by default or have to decouple from symphony framework and use it.
1)它涉及添加PHP库的安装和配置,因为YAML模块默认不来,或者必须与symphony框架分离并使用它。
2) Read and Write performance is worst among all config technologies, if compare to INI, XML, JSON . Taking lots more time read compare to INI file http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/
2)如果与INI,XML,JSON相比,所有配置技术中的读写性能最差。与INI文件相比,花费更多时间阅读http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/
3) Readability is not good, compare to INI and XML format. When it became large then difficult to read and manage with humane eyes.
3)与INI和XML格式相比,可读性不佳。当它变大时,很难用人性化的眼睛阅读和管理。
4) Bit bulky compare to INI and JSON.
4)与INI和JSON相比有点笨重。
So it’s better to use INI format rather than YAML.
所以最好使用INI格式而不是YAML。
#5
0
What I usually do is make an XML file and make a non dependent frontend to modifying the settings in the XML file.
我通常做的是创建一个XML文件,并使非依赖前端修改XML文件中的设置。
#6
0
use json good idea
for load config file
使用json好主意加载配置文件
"username" : "root" //in json file
$json = file_get_contents('path/to/file');
$data = json_decode($json);
$data->username; //print root
for write config file
用于写配置文件
$data['username'] = 'root';
if (file_put_contents('path/to/file', json_encode($dat))) { echo "<h4 class='alert alert-success'>config updated</h4>"; }
if(file_put_contents('path / to / file',json_encode($ dat))){echo“
config updated ”; }
last thing if you in web root use this .htaccess
最后一件事,如果你在web root中使用这个.htaccess
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
#1
25
NO
Personal experience. YAML seems a wonderful idea and I loved it and its simplicity. Then I started investing time on it: the very same concept of being able to read it in a language and write in another was very enticing, but... cutting it short, it turned up to be a mere illusion, unsubstantiated by facts.
个人经验。 YAML似乎是一个很棒的主意,我很喜欢它和简单。然后我开始把时间花在它上面:能够用一种语言阅读它并在另一种语言中写入的相同概念非常诱人,但是......简而言之,它变成了一种纯粹的幻觉,没有事实根据事实证明。
Every implementation of YAML differs too much from the other ones.
YAML的每个实现都与其他实现有很大不同。
- Arrays, automatically serialized by one, cannot sometimes be read by another.
- 由一个自动序列化的数组有时不能被另一个读取。
-
Cross references are supported, but their implementation is really sketchy.
支持交叉引用,但它们的实现非常粗略。
References are powerful, but:
参考文献很有用,但是:
- They are quite limited for some hardcore application.
- 对于某些硬核应用,它们非常有限。
- They represent an overkill to most low-end YAML-based projects.
- 它们代表了对大多数基于YAML的低端项目的过度杀伤力。
So, frequently, they are ignored, and errored upon, by most parsers.因此,大多数解析器经常会忽略它们并导致错误。
Summing it up, the standard isn't well set.
总结一下,标准设置不好。
There are core concepts that are nice and simple, but the actual standard document is full of details about features that most people don't want to use and are difficult and expensive to implement.
有一些核心概念很简单,但实际的标准文档中充满了大多数人不想使用的功能的详细信息,并且实现起来既困难又昂贵。
There isn't a distinction of levels of compatibility, like there is in DOM (DOM level 1, DOM level 2, etc) so every parser implementor implements what he feels like to, to the extent he can bear, then drops it and it's hard to discern what works and what doesn't.
没有区分兼容性级别,例如DOM(DOM级别1,DOM级别2等),因此每个解析器实现者都实现了他的感觉,在他能够承受的范围内,然后丢弃它并且它是很难辨别哪些有效,哪些无效。
Use Alternatives
-
JSON if you value the cross language data exchange language and little redundancy aspects as the top priority
如果您重视跨语言数据交换语言和少量冗余方面作为最高优先级,则为JSON
-
INI if you value performance and backward compatibility (on PHP, as
parse_ini_file()
is fast, and there since... always) and readability/editability by humans, instead.INI,如果你重视性能和向后兼容性(在PHP上,因为parse_ini_file()是快速的,并且因为......总是)和人类的可读性/可编辑性。
#2
4
My personal preference is a for a PHP based configuration file.
我个人的偏好是基于PHP的配置文件。
i know php so to me learning yaml just for the config files is extra work when you could have a simple config file like this, which in essence is no harder them yaml, and doesn't require a special interpreter library, just include('config.php') and you are away
我知道php所以对我来说学习yaml只是为了配置文件是额外的工作,当你可以有这样一个简单的配置文件,这本质上是没有更难他们yaml,并不需要一个特殊的解释器库,只是包括(' config.php')你离开了
$config = array(
'database' => array(
'default' => array(
'name' => 'dbname',
'host' => 'localhost',
'user' => 'username',
'pass' => 'password'
)
)
);
then you can reference config settings like this
然后你可以像这样引用配置设置
$host = $config['database']['default']['host'];
next step is to keep the config file simple, store the minimal amount of config data required, and then use the database to store the rest, and have admin screens for endusers to change settings within your application.
下一步是保持配置文件简单,存储所需的最少量配置数据,然后使用数据库存储其余数据,并为最终用户提供管理屏幕以更改应用程序中的设置。
#3
1
If you're writing a framework, then yes. You will have to end up doing more work on your part, but the goal of a framework is to make things easier for the person developing the application.
如果您正在编写框架,那么是的。您将不得不最终完成更多工作,但框架的目标是使开发应用程序的人员更容易。
Isn't Symfony moving away from YAML?
Symfony不是要离开YAML吗?
No, Symony2 is almost entirely configured by YAML.
不,Symony2几乎完全由YAML配置。
#4
1
I have dig bit more for config file format and found interesting facts for YAML format.
我为配置文件格式挖掘了更多,并为YAML格式找到了有趣的事实。
Mainly it have few drawback
主要是它有一些缺点
1) It involve installation and configuration of addition PHP library as YAML Module is not coming by default or have to decouple from symphony framework and use it.
1)它涉及添加PHP库的安装和配置,因为YAML模块默认不来,或者必须与symphony框架分离并使用它。
2) Read and Write performance is worst among all config technologies, if compare to INI, XML, JSON . Taking lots more time read compare to INI file http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/
2)如果与INI,XML,JSON相比,所有配置技术中的读写性能最差。与INI文件相比,花费更多时间阅读http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/
3) Readability is not good, compare to INI and XML format. When it became large then difficult to read and manage with humane eyes.
3)与INI和XML格式相比,可读性不佳。当它变大时,很难用人性化的眼睛阅读和管理。
4) Bit bulky compare to INI and JSON.
4)与INI和JSON相比有点笨重。
So it’s better to use INI format rather than YAML.
所以最好使用INI格式而不是YAML。
#5
0
What I usually do is make an XML file and make a non dependent frontend to modifying the settings in the XML file.
我通常做的是创建一个XML文件,并使非依赖前端修改XML文件中的设置。
#6
0
use json good idea
for load config file
使用json好主意加载配置文件
"username" : "root" //in json file
$json = file_get_contents('path/to/file');
$data = json_decode($json);
$data->username; //print root
for write config file
用于写配置文件
$data['username'] = 'root';
if (file_put_contents('path/to/file', json_encode($dat))) { echo "<h4 class='alert alert-success'>config updated</h4>"; }
if(file_put_contents('path / to / file',json_encode($ dat))){echo“
config updated ”; }
last thing if you in web root use this .htaccess
最后一件事,如果你在web root中使用这个.htaccess
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>