简体   繁体   English

覆盖Magento配置

[英]Override Magento Config

I am looking for a good solution to override the Magento config without changing the default values. 我正在寻找一个很好的解决方案来覆盖Magento配置而不更改默认值。

For example, I want to override the "web/unsecure/base_skin_url" item in the core_config_data table without deleting the existing value. 例如,我想覆盖core_config_data表中的“web / unsecure / base_skin_url”项而不删除现有值。 So if anywhere in the code this exact code is called: 因此,如果代码中的任何位置都会调用此确切代码:

Mage::getStoreConfig('web/unsecure/base_skin_url');

It will find the config option I set and not the default one... 它会找到我设置的配置选项,而不是默认的...

Thanks in advance! 提前致谢!

Chuck

Magento reads its configuration values at runtime directly from the configuration object's tree structure, so you need to use the configuration object's native setNode method to change the values. Magento直接从配置对象的树结构在运行时读取其配置值,因此您需要使用配置对象的本机setNode方法来更改值。 However, because of the way Magento loads in scoped configuration (self link), it's not as straight forward as it seems. 但是,由于Magento 在作用域配置中加载的方式(自我链接),它并不像看起来那么直截了当。

With current versions of Magento (and I believe, but have not tested, with older versions), you'll need to set the configuration value in the set of nodes for the current store . 使用当前版本的Magento(我相信,但尚未测试,使用旧版本),您需要在当前store的节点集中设置配置值。

Step one is getting the code for the currently set store. 第一步是获取当前设置的商店的代码。 You can do this programmatically with the following 您可以使用以下命令以编程方式执行此操作

$store = Mage::app()->getStore();
$code  = $store->getCode();

then, you can set a configuration value with the following call 然后,您可以使用以下调用设置配置值

$config = Mage::getConfig();
$config->setNode("stores/$code/web/unsecure/base_skin_url", 'value_to_set');

Keep in mind this all needs to happen after Magento has bootstrapped the configuration object. 请记住, Magento引导配置对象之后,所有这些都需要发生。 Also keep in mind there's a period of time where Magento will have a loaded configuration, but the store object will not be loaded. 另外请记住,Magento将有一段时间加载配置,但不会加载商店对象。 If this is the case you will not be able to load the store code from the store object. 如果是这种情况,您将无法从商店对象加载商店代码。

I did something similar in my Pulse Storm Chaos module . 我在Pulse Storm Chaos模块中做了类似的事情。 If you're interested in working code it's on Github . 如果您对编写代码感兴趣,请访问Github

Alan's answer is correct, but it doesn't care about the config cache. Alan的回答是正确的,但它并不关心配置缓存。 For example, if you call Mage::getStoreConfig('web/unsecure/base_skin_url') twice and change the value in between, the change has no effect. 例如,如果您两次调用Mage::getStoreConfig('web/unsecure/base_skin_url')并更改其间的值,则更改无效。 To get around this issue, you should use $store->setConfig('web/unsecure/base_skin_url', 'value_to_set') . 要解决此问题,您应该使用$store->setConfig('web/unsecure/base_skin_url', 'value_to_set') It does both: update the config cache and set the config node with Alan's method. 它同时执行这两项操作:更新配置缓存并使用Alan的方法设置配置节点。

If you want to overwrite some special config data, you can put it in app/etc/local.xml. 如果要覆盖某些特殊配置数据,可以将其放在app / etc / local.xml中。 But thats only useful for your own shop, not for public modules. 但这仅适用于您自己的商店,而不适用于公共模块。

Heres a way to overwrite the base_url for development-purposes without altering the database. 这是一种在不改变数据库的情况下覆盖base_url以进行开发的方法。

<config>
...
    <stores>
        <default>
            <web>
                <unsecure>
                    <base_url>http://dev.myshop.com/</base_url>
                </unsecure>
                <secure>
                    <base_url>http://dev.myshop.com/</base_url>
                </secure>
            </web>
        </default>
        <admin>
            <web>
                <unsecure>
                    <base_url>http://dev.myshop.com/</base_url>
                </unsecure>
                <secure>
                    <base_url>http://dev.myshop.com/</base_url>
                </secure>
            </web>
        </admin>
    </stores>
...
</config>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM