my_config.ini和my_config.php [英] my_config.ini vs my_config.php

查看:129
本文介绍了my_config.ini和my_config.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们使用.ini文件来设置变量,然后再调用框架的其余部分(我认为它是正确的

At work we use a .ini file to set variables prior to calling the rest of the framework (I think it goes

function getConfigVars(){
    //read my_config.ini file
    ....
    //call framework
}

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

and I have always wondered if there was a benefit to doing it that way.

在我看来,然后您必须编写访问规则以阻止人们从网络上查看它,而php必须对其进行解析并理解它.

It seems to me that you then have to write access rules to stop people from looking at it from the web and php has to parse it and understand it.

那么,为什么要使用my_config.ini而不是my_config.php?设置完成后,并不是每个人都应该去触摸它,似乎只要调用变量并可以在任何使用ini变量的地方使IDE自动完成文本/解析错误就更方便了.

So, why use my_config.ini rather than my_config.php? It is not like anyone should be touching it after it is set up and it seems more convenient to just call the variables and be able to have your IDE auto complete the text wherever you are using the ini variables / parse it for errors.

推荐答案

Zend Framework 包含配置解析解析以ini格式( Zend_Config_Ini ),听起来这就是您正在使用的.

The Zend Framework contains a config parses that parses files that are written in the ini format (Zend_Config_Ini), it sounds like this is what you are using.

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

The config file should not be located in your document root, and if it is not in your document root then no re-write rules are required since no-one can access it anyway.

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文件中设置到包含位置的不同路径.知道立即关闭所有开发内容,这使得将代码从开发转移到生产变得容易得多.

The Zend Framework uses it to allow you to have multiple configuration parameters, one for staging, one for development and one for production. This also allows for easy setting database settings for production, and for development and having two very different settings. Different paths set up in the ini file to where includes are located. This makes it much easier to move code from development to production knowing that immediately everything that is development will be turned off.

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

Sure, this would be possible with a PHP script, but it would require more parsing of the various configuration variables as well as doing if/then checks, whereas using the parse_ini_file() does all of this for you automatically.

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

The other answers also already pointed out that non-programmers may need to change variables and or something on the website that is set as a configuration variable (for example, site title that is used in the sites layout). INI files are easy to understand and read even for someone that has never programmed before.

我目前正在工作的网站上的示例:

Example from a website I am currently working on:

[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

对于可以在其中运行代码的各种环境,拥有多个数据集非常容易.

It makes it extremely easy to have multiple data sets for the various environments in which the code can be run.

这篇关于my_config.ini和my_config.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆