Zend 配置继承

发布于 2024-12-10 10:45:46 字数 1601 浏览 2 评论 0原文

我的 application.ini 中有这些值

[production]
; Database;
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user1"
    resources.db.params.password = "password1"
    resources.db.params.dbname = "projects__01"


;Names
    website.settings.websiteName = "My website 1"
    website.settings.websiteUrl = "http://www.mydomain1.com"
    website.settings.title = "mydomain.com - mydomain"
    website.settings.titleSeperator = " - "


[staging : production]
; Database;
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user2"
    resources.db.params.password = "password2"
    resources.db.params.dbname = "projects__02"

;Exceptions
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    resources.frontController.params.displayExceptions = 1

;Title and url
    website.settings.websiteName = "My website 2"
    website.settings.websiteUrl = "http://www.mydomain2.com"


[development : staging]
;Database
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user3"
    resources.db.params.password = "password3"
    resources.db.params.dbname = "projects__03"


;Title and url
    website.settings.websiteName = "My website 3"
    website.settings.websiteUrl = "http://www.mydomain3.com"

问题是所有数据库和异常值都正常工作,这意味着它们按照预期正确继承

但是我为 Title 和 url 设置的值没有正确继承,只有第一个定义的值被使用。

这是为什么呢?这是设计使然吗?是否仅继承预定义/标准环境值(例如数据库和异常)?

或者我在某个地方犯了错误?

I have these values in my application.ini

[production]
; Database;
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user1"
    resources.db.params.password = "password1"
    resources.db.params.dbname = "projects__01"


;Names
    website.settings.websiteName = "My website 1"
    website.settings.websiteUrl = "http://www.mydomain1.com"
    website.settings.title = "mydomain.com - mydomain"
    website.settings.titleSeperator = " - "


[staging : production]
; Database;
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user2"
    resources.db.params.password = "password2"
    resources.db.params.dbname = "projects__02"

;Exceptions
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    resources.frontController.params.displayExceptions = 1

;Title and url
    website.settings.websiteName = "My website 2"
    website.settings.websiteUrl = "http://www.mydomain2.com"


[development : staging]
;Database
    resources.db.adapter = "pdo_mysql"
    resources.db.params.host = "localhost"
    resources.db.params.username = "user3"
    resources.db.params.password = "password3"
    resources.db.params.dbname = "projects__03"


;Title and url
    website.settings.websiteName = "My website 3"
    website.settings.websiteUrl = "http://www.mydomain3.com"

The problem is all database and exception values work properly, meaning that they are inheriting properly as they are supposed to

But the values I have set for Title and url do not inherit properly, only the first defined ones are used.

Why is this? is this by design? are only predefined/standard environment values such as database and exceptions are inherited?

Or am I making a mistake somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女猫性小仙女 2024-12-17 10:45:46

好的,从您的评论来看,您似乎正在引导程序中创建一个新的 Zend_Config 对象,并将其放入注册表中,而这并没有返回您所期望的结果。如果是这种情况,我猜测您省略了配置对象上的第二个参数,因此您有这样的内容:

$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');

但您应该拥有的更像是这样的:

$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);

第二个参数告诉它配置文件的哪个部分使用,否则它将始终获得相同的值。

然而,您实际上不需要重新解析配置文件,因为 Zend Application 已经完成了这项工作。相反,您可以从引导类访问选项并使用它们来创建对象(或者仅将选项存储在现有的数组格式中):

protected function _initConfig()
{
    $config = new Zend_Config($this->getOptions());
    Zend_Registry::set('config', $config);

    return $config;
}

这应该按您的预期工作。

如果我的猜测是错误的,请您编辑您的问题以包含引导程序的相关部分,其中创建配置对象并将其存储在注册表中。

Okay, from your comment it sounds like you are creating a new Zend_Config object in the bootstrap, putting this in the registry, and it's this that's not returning what you'd expect. If this is the case I'm guessing that you've omitted the second parameter on the config object, so you have something like this:

$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');

but what you should have is more like this:

$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);

the second param tells it what section of the config file to use, without that it will always get the same value.

However, you don't actually need to re-parse the config file since Zend Application has done this already. Instead you can access the options from the bootstrap class and use these to create an object (or just store the options in their existing array format):

protected function _initConfig()
{
    $config = new Zend_Config($this->getOptions());
    Zend_Registry::set('config', $config);

    return $config;
}

and this should work as you expect.

If my guess was wrong, please can you edit your question to include the relevant part of your bootstrap where the config object is created and stored in the registry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文