您好,欢迎访问一九零五行业门户网

Magento中的个性化后端配置

在本教程中,我将演示 magento 后端的自定义模块配置。在后端提供一组模块的可配置选项总是有用的,这允许管理员轻松配置模块特定的设置。我们将通过在 magento 中创建自定义模块来逐步完成设置模块配置的过程。
magento 中的自定义配置简介作为一名开发人员,在创建自定义模块的过程中,您可能会觉得如果能够将某些模块特定设置的控制权转移到后端就好了。 magento 允许您使用基于 xml 文件的配置系统来实现这一点。您只需按照 magento 的约定设置文件,它们就会自动神奇地被拾取!大多数时候,您在设置基本前端存储时需要处理核心模块提供的大量可配置设置。
我假设您熟悉 magento 中的基本模块创建过程。如果您想了解有关 magento 中自定义模块的更多信息,这是一篇不错的文章。话虽如此,magento 遵循约定优于配置的范例,如果您是自定义模块开发过程的新手,这应该会让事情更容易理解。
创建自定义模块以提供自定义配置在本教程中,我将创建一个名为“customconfig”的基本自定义模块,该模块在系统 > 配置菜单下提供后端配置。以下是所需设置所需的文件列表:
app/etc/modules/envato_all.xml:这是一个用于启用我们的自定义模块的文件。app/code/local/envato/customconfig/etc/config.xml:这是一个模块配置文件。app/code/local/envato/customconfig/etc/system.xml:这是一个声明我们模块的配置选项的文件。app/code/local/envato/customconfig/model/options.php:这是一个模型文件,为配置的某些表单元素提供选项。app/code/local/envato/customconfig/helper/data.php:这是 magento 翻译系统使用的文件。文件设置首先,我们将创建一个模块启用程序文件。创建文件“app/etc/modules/envato_all.xml”并将以下内容粘贴到该文件中。我们使用“envato”作为模块命名空间,使用“customconfig”作为模块名称。默认情况下它将启用我们的“customconfig”模块。
<?xml version=1.0?><config> <modules> <envato_customconfig> <active>true</active> <codepool>local</codepool> </envato_customconfig> </modules></config>
接下来,我们需要创建一个模块配置文件。创建“app/code/local/envato/customconfig/etc/config.xml”并将以下内容粘贴到该文件中。
<?xml version=1.0?><config> <modules> <envato_customconfig> <version>0.0.1</version> </envato_customconfig> </modules> <global> <helpers> <customconfig> <class>envato_customconfig_helper</class> </customconfig> </helpers> <models> <customconfig> <class>envato_customconfig_model</class> </customconfig> </models> </global> <adminhtml> <acl> <resources> <admin> <children> <system> <children> <config> <children> <customconfig_options> <title>custom configuration section</title> </customconfig_options> </children> </config> </children> </system> </children> </admin> </resources> </acl> </adminhtml></config>
您应该熟悉 magento 约定的模型和辅助类声明。当然, 标签对您来说是新的,这是我们在本教程中关心的内容。让我们详细了解每个标签以了解其代表的含义。
标签用于定义admin端的资源。在我们的示例中,模块的配置页面是一种资源。此外,我们使用 标签来包装 标签,这意味着只有经过身份验证的用户才能访问它。
接下来的几个标签用于定义后端配置页面的路径。所以我们的配置页面的路径看起来像“admin/system/config/customconfig_options”。当然,最后一个标签 应该是唯一的,这样就不会与其他扩展冲突。
接下来,让我们定义最重要的文件“system.xml”。创建“app/code/local/envato/customconfig/etc/system.xml”并将以下内容粘贴到该文件中。
<?xml version=1.0?><config> <tabs> <customconfig translate=label module=customconfig> <label>custom configuration tab</label> <sort_order>1</sort_order> </customconfig> </tabs> <sections> <customconfig_options translate=label module=customconfig> <label>custom configuration settings</label> <tab>customconfig</tab> <frontend_type>text</frontend_type> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <section_one translate=label> <label>section one</label> <frontend_type>text</frontend_type> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <custom_field_one> <label>custom text field</label> <frontend_type>text</frontend_type> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <comment>example of text field.</comment> </custom_field_one> </fields> </section_one> <section_two translate=label> <label>section two</label> <frontend_type>text</frontend_type> <sort_order>2</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <custom_field_two> <label>custom select field</label> <frontend_type>select</frontend_type> <source_model>customconfig/options</source_model> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <comment>example of select field.</comment> </custom_field_two> <custom_field_three> <label>custom radio field</label> <frontend_type>radios</frontend_type> <source_model>customconfig/options</source_model> <sort_order>2</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <comment>example of radios field.</comment> </custom_field_three> <custom_field_four> <label>custom multiselect field</label> <frontend_type>multiselect</frontend_type> <source_model>customconfig/options</source_model> <sort_order>3</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <comment>example of multiselect field.</comment> </custom_field_four> </fields> </section_two> </groups> </customconfig_options> </sections></config>
正如你可能已经猜到的,这个文件负责在后端显示我们模块的配置页面。让我们仔细看看该文件中的一些重要标签。
标签用于定义将显示在配置页面左侧的选项卡。 标签用于定义每个选项卡下的部分。
让我们尝试通过一个例子来理解它。转到 magento 后端的系统 > 配置。您会注意到有不同的标签,例如常规、目录、客户等。
在每个选项卡下,您还会看到不同的部分。例如,高级标签下有管理、系统、高级和开发人员部分。在我们的例子中,我们将在自定义配置选项卡下有一个自定义配置设置部分。
接下来,我们使用 标签将相似的配置字段分组在一起。例如,您希望在不同部分下显示图像相关字段和电子邮件相关字段。为此,我们定义了 和 标签。
最后,我们使用 标签包裹每个部分的字段。在我们的示例中,我们在“第一部分”字段集下提供了一个文本字段,而选择框、单选按钮和多项选择等其他字段则在“第二部分”字段集下可用。这就是“system.xml”文件。
接下来,让我们创建一个模型文件“app/code/local/envato/customconfig/model/options.php”。
<?phpclass envato_customconfig_model_options{ /** * provide available options as a value/label array * * @return array */ public function tooptionarray() { return array( array('value'=>1, 'label'=>'one'), array('value'=>2, 'label'=>'two'), array('value'=>3, 'label'=>'three'), array('value'=>4, 'label'=>'four') ); }}
这里没有什么特别的 - 它只是用于将选项提供给单选表单字段并在配置表单中选择表单字段。
最后,我们需要创建“app/code/local/envato/customconfig/helper/data.php”文件,以确保 magento 的翻译系统正常工作。它几乎是一个空文件,但按照约定应该存在!
<?php/** * sample widget helper */class envato_customconfig_helper_data extends mage_core_helper_abstract{}
我们已经完成了文件设置。在下一节中,我们将检查它在 magento 后端的外观。
浏览后端以测试自定义配置前往 magento 后端并清除所有缓存。可以通过转至系统 > 缓存管理来访问它。
现在,转到系统 > 配置打开配置页面。您应该会注意到页面左侧的自定义配置选项卡。在其下方,可以使用自定义配置设置链接,单击该链接将打开我们模块的配置页面。如果您看不到它,请尝试退出管理部分并重新登录。
这是一个开箱即用的漂亮界面,不是吗?您可以尝试一下,填写表单并点击保存配置按钮来提交更改,magento 将处理剩下的事情。
要访问代码中配置参数的值,可以使用 getstoreconfig 静态方法。例如,您可以使用以下语法检索 custom_field_one 参数的值:
mage::getstoreconfig('customconfig_options/section_one/custom_field_one');
语法相当简单 - 您需要使用 system.xml 文件中定义的“section_name/group_name/field_name”模式。
结论管理系统配置是 magento 最强大、最有用的部分之一。 magento 允许您非常轻松地插入自定义配置参数,这要归功于最强大的电子商务系统之一的可扩展性!使用下面的提要分享您的想法!
以上就是magento中的个性化后端配置的详细内容。
其它类似信息

推荐信息