MAGENTO与模块的配置文件
基本的MAGENTO module configuration
module configuration 各种指令的说明
MAGENTO module的配置具体应该包括有如下的几个方面:
基本配置
定义一个module,并让它启用
module体系在xml配置文件中的应用
没有front route(没有前端控制器)
有front route
rewrite already controller or block (重新实现一个已经存在的控制器或者block)
对数据库进行处理
新的实体配置
实体的定义以及关系
事件监听
事件监听以及observer
与后台处理
在后端添加路由
对后台菜单添加菜单项
为backend configuration添加可配置项,进而获得以及处理可以获得的数据
国际化与本地化
MAGENTO module一般所涉及到的配置文件以及命名规范
定义一个module,并让它启用
在app\etc\modules目录下新建一个xml文件其中来定义module名称.
文件名规范为: CompanyName(首字母大写)+underscore(下划线)+MoudleName(模块名称,首字母大写).xml
demo: CompanyName_Module.xml (Book_Brands.xml or PACKT_News.xml)
KEYS : CompanyName_Module = package
package PACKT_News
true</active>
<codePool>local</codePool>
</CompanyName_Module>
</modules>
如:
true</active>
<codePool>local</codePool>
</PACKT_News>
说明: “codePool” tag define module is local Edition or community Edition,它的所选值包括有:
core: The Magento core code resides here
community: This contains the code installed via Magento Connect
local: This contains the custom module's code
KEY: ensure module 已经启用,在 configuration –>ADVANCED –> Advanced 的module值是enable的。
module体系在xml配置文件中的应用
MAGENTO module的文件结构就不需要多说了,这里指完整的情况(如果是rewrite或者是只需要处理block以及template的情况除外):
文件结构
• Block/
• controllers/
• etc/
• Model/
Mysql4/
• sql/
文件结构以及路径: code\local\Packt\Socialwidget ---> 公司名下有Module名
文件以及类名的命名规范
针对module的文件以及类名的命名规范均遵守Zend Framework的命名规范:
关键类名与文件名保持一致。
Controller
1.前台(fontend) Controller类名的定义规范: package(公司名+module名)+Controller名(与controller文件名一致)+Controller(remain keys words),如:
package名
extends Mage_Core_Controller_Front_Action {
}2. 后台(backend) Adminhtml后台Controller的命名规范: package(公司名+module名)+Adminhtml(目录)+Controller名(与controller文件名一致)+Controller(remain keys words),如:
extends Mage_Adminhtml_Controller_action {
}
Block
1.前台(fontend) Block 类名的定义规范: package(公司名+module名)+Block(remain keys words)+Block名(与Block文件名一致),如:
package+ Block(keyword)+Block名(与文件名一致)
extends Mage_Core_Block_Template {
}
extends Mage_Core_Model_Abstract {
}extends Varien_Object {
}
Helper
Helpr的通用helper为: Data.php,其命名规范为: package(公司名+module名)+Helper(remain keys words)+Helper名(与Helper文件名一致),Helper_Data是必须存在的。
extends Mage_Core_Helper_Abstract {
}
当然也可以再提供多个Helper,文件命名规范以及类名遵守规范即可。
Module与xml中的对应关系(config.xml)
1.确定module信息,包括版本,module名(相关联)。 它可以分成两种:
一种为独立的module,与其它的module没有任何的依赖关系
另外一种则为有依赖关系的module,它与其它的module有依赖关系
<modules>
<CompanyName_Module>
<version>1.0.0</version>
</CompanyName_Module>
</modules>
2. 关联block,helper,model等(控制器属于路由层)
<global>
<models>
<modulename>(小写)>
<class>package_Model</class>
</modulename>
</models>
<blocks>
<modulename(小写)>
<class>package_Block</class>
</modulename>
</blocks>
<helpers>
<modulename(小写)>
<class>package_Helper</class>
</modulename>
</helpers>
</global>
如: PACKT_News为package名,News则为module name,这里应该小写
global>
Model与数据库层的关系,只要存在model,就存在着ORM。其中model可以分成三种情况:
1.没有与任何独立表相关联,在使用中更多的是使用model的继承机制。
2.model对应单独的数据库表,一般存在着建表。这种形式的model为mysql4,数据库的设计使用的是非EAV的方式(数据库处理依然使用PDO的方式),即Normalization。
3.model使用entity的设计思路,即EAV。
- Normalization的方式[MYSQL4]:
<global>
<models>
<modulename>
<class>package_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
<modulename_mysql4>
<class>package_Model_Mysql4</class>
<entities>
<modulename>
<table>TableName</table>
</modulename>
</entities>
</modulename_mysql4>
</models>
<!-- turn on database connections –>
<resources>
<!-- setup is needed for automatic installation –>
<modulename_setup>
<setup>
<module>package</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</modulename_setup>
<!—this configuration is needed for write connection–>
<modulename_write>
<connection>
<use>core_write</use>
</connection>
</modulename_write>
<!—this configuration is needed for read connection–>
<modulename_read>
<connection>
<use>core_read</use>
</connection>
</modulename_read>
</resources>
</global>
DEMO(example)
global>
TIPS: 1. resourceModel中的值为mysql4,则说明对于ORM的处理,会使用MYSQL4(非EAV)的方式来完成。
2.modulename_setup则说明系统会自动的安装SQL文件。
3. 使用MYSQL4的方式所产生的数据库操作,必须要考虑MYSQL4,因此必须create another model for MySQL4 version. 新建一个mysql4的目录放置于model下,
MYSQL4的model文件命名为: package(公司名+module名)+Model(remain keys words)+Mysql4(remain key words)+module名(与module文件名一致),它继承于Mage_Core_Model_Mysql4_Abstract,同时指定主键。
如:
class Packt_News_Model_Mysql4_News extends Mage_Core_Model_Mysql4_Abstract
{
}
文件的命名: 考虑到自动安装,modulename_setup即为目录名,里面包括了mysql的安装或者是升级脚本.
安装脚本的命名规范: mysql4-install-moduleversion(模块的版本号).php
升级脚本的命名规范:
EAV的方式【重点总结】:
一个EAV设计,最基本由四个部分构成: model, entity,entity_Attributes,attribute values。 也就是说一个基于EAV设计的model需要四个class.
<models>
<modulename>
<class>package_Model</class>
<resourceModel>modulename_entity</resourceModel>
</modulename>
<modulename_entity>
<class>package_Model_Entity</class>
<entities>
<entity>
<table>TableName</table>
</entity>
<eav_attribute>
<table>customer_eav_attribute</table>
</eav_attribute>
</entities>
</modulename_entity>
</models>
对于数据库连接的说明
继承机制与rewrite(重写原有的模块)
对于controller的重写---->将原有提交的action换成一个新的action.如何来处理业务逻辑
对于model,block的重写
1. Block rewrite: 将原有的显示模块rewrite,以通过控制block,来对表现层进行模块化
<blocks>
<modulename>
<rewrite>
<original block Class>all full new block class</original block Class>
</rewrite>
</modulename>
</blocks>
比如;
<blocks>
<catalog>
<rewrite>
<product_view_type_simple>Company_NewProduct_Block_ProductViewCase</product_view_type_simple>
</rewrite>
</catalog>
</blocks>
对于block进行重写,BLOCK类是必须要进行继承的.
extends Mage_Catalog_Block_Product_View_Type_Simple {
}
<reference name="product.info.media">
<action method="setTemplate" ifconfig="ig_lightbox/general/enabled"><template>ig_lightbox/media.phtml</template></action>
</reference>
直接替换掉template即可。
MAGENTO与default configuration –> model adapt
为backend configuration添加可配置项,进而获得以及处理可以获得的数据
backend configuration的配置,主要是定义在system.xml中,并且具体的定义在sections pair tags中
configuration 添加可配置主要包括了:
为一个已经存在的子配置项添加输入等项
<carriers>
<groups>
<freeshipping translate="label">
<fields>
<account translate="label">
<label>Shipping Company Website</label>
<frontend_type>text</frontend_type>
<sort_order>21</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</account>
</fields>
</freeshipping>
</groups>
</carriers>
添加一个accordion项给菜单
demo code
<carriers>
<groups>
<!-- new accordion menu ---->
<myshipping translate="label" module="shipping">
<!— it show the accordion item –>
<label>Canada Post</label>
<frontend_type>text</frontend_type>
<sort_order>13</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<account translate="label">
<label>Account number</label>
<frontend_type>text</frontend_type>
<sort_order>7</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</account>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</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>
</active>
</fields>
</myshipping>
</groups>
</carriers>
添加一个项到导航菜单中
<featuredproducts translate="label" module="featuredproducts">
<class>separator-top</class>
<!—special the tab name –>
<label>Featured Products</label>
<!— add this feature to tab –>
<tab>inchoo</tab>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<general translate="label">
<label>General</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>
<heading translate="label comment">
<label>Heading</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</heading>
</fields>
</general>
</groups>
</featuredproducts>
添加整个模块项到导航菜单块中
<!-- 定义一个tab项—>
<tabs>
<!--
<tabName module="Module Name">
<label>Inchoo</label>
<sort_order>110</sort_order>
</tabName >
</tabs>
如
<tabs>
<inchoo module="inchoo_notes">
<label>Inchoo</label>
<sort_order>110</sort_order>
</inchoo>
</tabs>
<sections>
</sections>
在导航中添加图像链接项
为accordion添加文字或者是图片内容
对于这几种情况的实现,大多数是通过comment来实现的.
<mxperts translate="label" module="mxperts">
<label>jQuery</label>
<class>jquerybase</class>
<tab>jqueryall</tab>
<sort_order>340</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<jquerysettings translate="label">
<label>jQuery Settings</label>
<comment><![CDATA[
<a href="http://www.mxperts.de" target="_blank"><IMG border="0" src="http://www.mxperts.de/mxpertsjQuery.jpg"></a>
<div style="margin-top:4px;margin-bottom:4px; color:gray;">Jquery-Base: the modified jQuery-Script from <a href="http://jquery.com/" target="_blank">http://jquery.com/</a> to run jQuery in noConflict() mode. </div>
<div style="margin-top:4px;"><b>TMEDIA cross communications, <a href="mailto:info@tmedia.de">info@tmedia.de</a></b><br />
Johannes Teitge, Daniel Sasse, Igor Jankovic<br /><br /></div>
<hr /><br />
]]>
</comment>
<frontend_type>text</frontend_type>
<sort_order>9</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<!-- Status des Moduls -->
<active translate="label">
<!-- Bezeichnung des Eingabefeldes -->
<label>Enabled</label>
<!-- Kommentar -->
<comment><![CDATA[<b>enable</b> or <b>disable</b> jQuery.]]></comment>
<!-- Eingabetyp (text,select,mutliselect, text, texarea, etc...) -->
<frontend_type>select</frontend_type>
<!-- Model welches die Inhalte dere Auswahl liefert -->
<source_model>adminhtml/system_config_source_yesno</source_model>
<!-- Position im Backend -->
<sort_order>0</sort_order>
<!-- Anzeige der jeweiligen Konfig.-Bereiches -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
</fields>
</jquerysettings>
Q:如何添加可添加配置项的父元素名称是什么?
对于项目中各种形式的显现:
单选,复选,下拉,菜单列表等
预设或者获得configuration中的数据值?
setConfigData 获得configuration值
预设值的情况更多出现在module中sql目录下的setup中。如
$installer = $this;
$installer->startSetup();
$installer->setConfigData('explodedmenu/home_link/enable', 1);
getConfigData获得configuration的值
$this->getConfigData('handling');
后台菜单添加菜单项