微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

my_config.ini和my_config.php

在工作中,我们使用.ini文件调用框架的其余部分之前设置变量(我认为它是
function getConfigVars(){
    //read my_config.ini file
    ....
    //call framework
}

我一直想知道这样做是否有好处.

在我看来,你必须编写访问规则来阻止人们从Web上查看它,并且PHP必须解析它并理解它.

那么为什么要使用my_config.ini而不是my_config.PHP?它不是任何人应该触摸它,它设置后,似乎更方便的只是调用变量,并能够让你的IDE自动完成文本,无论你使用ini变量/解析它的错误.

Zend Framework包含一个配置解析,解析以ini格式( Zend_Config_Ini)编写的文件,这听起来像这是你正在使用的.

配置文件不应位于文档根目录中,如果不在您的文档根目录中,则不需要重写规则,因为无论如何,无法访问它.

The INI format is specialized to provide both the ability to have a hierarchy of configuration data keys and inheritance between configuration data sections. Configuration data hierarchies are supported by separating the keys with the dot or period character (.). A section may extend or inherit from another section by following the section name with a colon character (:) and the name of the section from which data are to be inherited.

Zend_Config_Ini页.

Zend Framework使用它来允许您拥有多个配置参数,一个用于分段,一个用于开发,一个用于生产.这也可以轻松设置数据库生产设置,并进行开发,并具有两个非常不同的设置.将ini文件中设置的不同路径放置到包含的位置.这使得将代码从开发转移到生产变得更加容易,因为知道开发中的一切都将被关闭.

当然,这可以通过PHP脚本来实现,但它需要对各种配置变量进行更多的解析以及如果/ then检查,而使用parse_ini_file()可以自动执行所有这些操作.

其他答案也已经指出,非程序员可能需要更改设置为配置变量的网站上的变量或某些东西(例如,在站点布局中使用的站点标题). INI文件对于以前从未编程过的人来说很容易理解和阅读.

我正在从事的一个网站的例子:

[production]
PHPSettings.display_startup_errors = 0
PHPSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.PHP"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.db.adapter       = "PDO_sqlITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/users.db"

resources.view[] =

[staging : production]

[testing : production]
PHPSettings.display_startup_errors = 1
PHPSettings.display_errors = 1
resources.db.params.dbname = APPLICATION_PATH "/../data/db/users-testing.db"

[development : production]
PHPSettings.display_startup_errors = 1
PHPSettings.display_errors = 1
resources.db.params.dbname = APPLICATION_PATH "/../data/db/users-dev.db

它使得为可以运行代码的各种环境拥有多个数据集非常容易.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐