magento开发--入门深入理解第一章

本文深入介绍了Magento配置系统的核心原理及其实现方式,包括如何通过创建模块来扩展或覆盖系统行为,以及如何利用配置文件实现对系统高度灵活的定制。

第一章 - Magento重点内容强大的配置系统
Magento的配置系统就像是Magento的心脏,支撑着Magento的运行。这套配置系统掌管着几乎所有“module/model/class/template/etc”。它把整个Magento系统抽象出来,用一个配置文件来描述。这里的“配置文件”并不是一个物理上存在的文件,而是Magento根据当前的系统状态动态生成的一段XML。大多数的PHP开发者并不习惯于这样抽象层,因为它增加的编程的复杂性。但是这样的抽象提供了无与伦比的灵活性,允许你覆盖几乎任何系统的默认行为。

首先,让我们写一个简单的插件来看看这个所谓的“配置文件”长什么样。虽然我已经提供的现成的代码,但是还是建议你自己建立这个插件,把整个流程走一遍有助于你的理解。
我们将要创建一个Magento的模块。Magento的模块由php和xml文件组成,目的是扩展或者覆盖系统的行为,比如为订单增加数据模型,更改一个类的方法,或者增加一个全新的功能。

大多数Magento的系统模块的结构和我们将要构建的插件的结构是一样的。Magento的系统模块在以下目录
app/code/core/Mage
每一个子目录都是一个单独的模块。这些模块是由Magento官方开发的。我们安装完Magento以后,所使用的功能就是来自这些模块。我们自己创建的模块应该放在如下目录
app/code/local/Packagename
“Packagename”应该是一个唯一的字符串,用来标识你的代码。通常人们使用公司名字作为Packagename,比如
app/code/local/Microsoft
这里我就使用“Alanstormdotcom”。 然后,我们要创建以下目录结构
app/code/local/Alanstormdotcom/Configviewer/Block
app/code/local/Alanstormdotcom/Configviewer/controllers
app/code/local/Alanstormdotcom/Configviewer/etc
app/code/local/Alanstormdotcom/Configviewer/Helper
app/code/local/Alanstormdotcom/Configviewer/Model
app/code/local/Alanstormdotcom/Configviewer/sql
你的插件并不一定需要包含以上所有的目录,但是为了以后开发方便,我们还是在一开始就把目录创建好。接下来我们要创建两个文件,一个是config.xml,放在etc目录下面
app/code/local/Alanstormdotcom/Configviewer/etc/config.xml
文件内容如下

<config>      
    <modules>  
        <Alanstormdotcom_Configviewer>  
            <version>0.1.0</version>  
        </Alanstormdotcom_Configviewer>  
    </modules>  
</config>  

第二个文件需要在如下位置创建
app/etc/modules/Alanstormdotcom_Configviewer.xml
第二个文件应该遵循如下命名规则“Packagename_Modulename.xml”,文件内容如下

<config>  
    <modules>  
        <Alanstormdotcom_Configviewer>  
            <active>true</active>  
            <codePool>local</codePool>  
        </Alanstormdotcom_Configviewer>  
    </modules>  
</config>  

我们先不管这些文件是干什么的,以后会解释。建立好这两个文件以后,你的模块的骨架就已经完成了。Magento已经知道你的模块存在,但是现在你的模块不会做任何事情。我们来确认一下Magento确实装载了你的模块
清空Magento缓存
在后台管理界面,进入 System->Configuration->Advanced
展开“Disable Modules Output”
确认“Alanstormdotcom_Configviewer”显示出来了
如果你看到“Alanstormdotcom_Configviewer”,那么恭喜你,你已经成功创建了你第一个Magento模块!

创建模块逻辑

我们之前创建的模块不会做任何事情,下面我们来为这个模块加入逻辑
1. 检查“showConfig”查询字符串是否存在
2. 如果“showConfig”存在,那么检查“showConfigFormat”查询字符串是否存在
3. 如果“showConfigFormat”存在,那么输出指定格式的配置信息,否则输出默认格式的配置信息
4. 终止执行流程
首先更改我们的config.xml文件

<config>  
    <modules>...</modules>  
    <global>  
        <events>  
            <controller_front_init_routers>  
                <observers>  
                    <alanstormdotcom_configviewer_model_observer>  
                        <type>singleton</type>                            
                                                                   <class>Alanstormdotcom_Configviewer_Model_Observer</class>  
                        <method>checkForConfigRequest</method>  
                    </alanstormdotcom_configviewer_model_observer>  
                </observers>  
            </controller_front_init_routers>  
        </events>  
    </global>  
</config> 

然后创建如下文件
Alanstormdotcom/Configviewer/Model/Observer.php
输入以下内容

<?php  
    class Alanstormdotcom_Configviewer_Model_Observer {  
        const FLAG_SHOW_CONFIG = 'showConfig';  
        const FLAG_SHOW_CONFIG_FORMAT = 'showConfigFormat';       
        private $request;  
        public function checkForConfigRequest($observer) {            
            $this->request = $observer->getEvent()->getData('front')->getRequest();  
            if($this->request->{self::FLAG_SHOW_CONFIG} === 'true'){  
                $this->setHeader();  
                $this->outputConfig();  
            }  
        }  
        private function setHeader() {  
            $format = isset($this->request->{self::FLAG_SHOW_CONFIG_FORMAT}) ?   
            $this->request->{self::FLAG_SHOW_CONFIG_FORMAT} : 'xml';                                  
            switch($format){  
                case 'text':  
                    header("Content-Type: text/plain");  
                    break;  
                default:  
                    header("Content-Type: text/xml");  
            }             
        }  
        private function outputConfig() {              
            die(Mage::app()->getConfig()->getNode()->asXML());        
        }  
    }  
?> 

好了,代码编辑结束。清空你的Magento缓存,输入如下URL
http://magento.example.com/?showConfig=true

配置文件分析

打开上述URL,你应该看到一个巨大的XML文件。这个文件描述了当前Magento系统的状态。它列出了所有的模块,数据模型,类,事件,监听器等等。举个例子,如果你搜索如下字符串
Configviewer_Model_Observer
你会发现刚刚你创建的那个类被列出来了。Magento会解析每个模块的config.xml,并把它们包含在这个全局配置中。

这个配置文件有啥用?

到目前为止,我们所作的事情似乎没什么意义,但是这个配置文件却是理解Magento的关键因素。你创建的每一个模块都会被加到这个配置文件中,任何时候,你需要调用一个系统功能的时候,Magento都会通过这个配置文件来查询相应的模块和功能。举个简单的例子,如果你懂MVC的话,你应该和“helper class”之类概念的打过交道

$helper_salesrule = new Mage_SalesRule_Helper();

Magento抽象了PHP的类声明方式。在Magento系统中,上面的代码等同于
$helper_salesrule = Mage::helper(‘salesrule’);
Magento将通过以下逻辑来处理这行代码
在配置文件中查找<helpers />标签
<helpers />里面查找 <salesrule />标签
<sales />里面查找 <class />标签
实例化从#3找到的类(Mage_SalesRule_Helper)
Magento总是通过配置文件来获得类名,这个逻辑看起来有些复杂,但这样做的优点也很明显,我们可以不需要更改Magento的代码就能更改Magento的核心功能。

一章:Magento介绍...................................................................................................................4 Magento 的特色......................................................................................................................5 什么是Magento........................................................................................................................6 Magento的元素和专业术语....................................................................................................6 网站和商店(website and store)...................................................................................7 网站(website)..............................................................................................................7 商店(store)..................................................................................................................7 商店界面(store views)................................................................................................7 Magento的程序架构................................................................................................................8 内核(Core)..................................................................................................................9 本地的(Local).............................................................................................................9 社区(Community).......................................................................................................9 扩展(Extensions).........................................................................................................9 模块(Modules)............................................................................................................9 界面(Interface)..........................................................................................................10 主题(Themes)............................................................................................................10 区块(Blocks).............................................................................................................11 第二章:Magento入门.................................................................................................................12 Magento的系统需求..............................................................................................................12 Magento下载..........................................................................................................................13 Magento安装和配置..............................................................................................................14 Magento后台控制面板介绍..................................................................................................14 创建多网站和商店(Creating Multiple Websites and Stores)...................................14 缓存管理(Cache Management).................................................................................16 第三章:建立目录........................................................................................................................17 产品目录概念总览................................................................................................................17 设置默认项....................................................................................................................17 产品图片存放目录(Product Image Placeholders)....................................................19 创建分类(Creating Categories).................................................................................21 分类中添加产品(Assigning products at the category level)....................................24 定制分类页面的外观(Assigning designs at the category level)..............................25 在分类页面中使用静态区块(Using static blocks with categories).........................26 属性(Attributes)................................................................................................................27 创建属性(Creating an Attribute)...............................................................................27 特性(Properties)........................................................................................................27 管理标记/选项(Manage Label/Options)...................................................................30 管理属性集(Managing Attribute Sets).....................................................................31 产品(Procucts)..................................................................................................................34
Magento用户手册目录 第一章Magento介绍...................................................................................................................4 Magento 的特色......................................................................................................................5 什么是Magento........................................................................................................................6 Magento的元素和专业术语....................................................................................................6 网站和商店(website and store)...................................................................................7 网站(website)..............................................................................................................7 商店(store)..................................................................................................................7 商店界面(store views)................................................................................................7 Magento的程序架构................................................................................................................8 内核(Core)..................................................................................................................9 本地的(Local).............................................................................................................9 社区(Community).......................................................................................................9 扩展(Extensions).........................................................................................................9 模块(Modules)............................................................................................................9 界面(Interface)..........................................................................................................10 主题(Themes)............................................................................................................10 区块(Blocks).............................................................................................................11 第二章:Magento入门.................................................................................................................12 Magento的系统需求..............................................................................................................12 Magento下载..........................................................................................................................13 Magento安装和配置..............................................................................................................14 Magento后台控制面板介绍..................................................................................................14 创建多网站和商店(Creating Multiple Websites and Stores)...................................14 缓存管理(Cache Management).................................................................................16 第三章:建立目录........................................................................................................................17 产品目录概念总览................................................................................................................17 设置默认项....................................................................................................................17 产品图片存放目录(Product Image Placeholders)....................................................19 创建分类(Creating Categories).................................................................................21 分类中添加产品(Assigning products at the category level)....................................24 定制分类页面的外观(Assigning designs at the category level)..............................25 在分类页面中使用静态区块(Using static blocks with categories).........................26 属性(Attributes)................................................................................................................27 创建属性(Creating an Attribute)...............................................................................27 特性(Properties)........................................................................................................27 管理标记/选项(Manage Label/Options)...................................................................30 管理属性集(Managing Attribute Sets).....................................................................31 产品(Procucts)..................................................................................................................34 设定配置和属性(Setting up configuration and Attributes)......................................35 创建简单产品(Creating a Simple Product)..............................................................36 创建可配置产品(Creating a Configurable Product).................................................45 创建分组产品(Creating a Grouped Product)............................................................49 相关产品,推荐销售,交叉销售 (Related Products,Up-sells,Cross-sells) 52 产品比较功能(Product Comparisons)......................................................................54 层级导航(Layered Navigation).................................................................................55 产品级别制定外观(Assigning Designs at the product level)...................................57 库存管理...............................................................................................................................58 建立存货管理默认选项(Creating stock management default options)...........................58 产品批量更新........................................................................................................................58 URL重写...............................................................................................................................59 第四章:为销售做准备................................................................................................................62 结账流程(Checkout Process)............................................................................................62 本地化设置(Localization Settings)...................................................................................69 地区设置(Locale Settings)........................................................................................70 货币设置(Currency Configuration)..........................................................................70 货币汇率(Currency Rates)........................................................................................72 Taxes设置...............................................................................................................................72 税率(Tax Rates)........................................................................................................72 税类(Tax Classes).....................................................................................................74 税收规则(Tax Rules)................................................................................................75 发货选项(Shipping Options)............................................................................................76 发货设置(Shipping Setting)......................................................................................76 发货方法(Shipping Methods)...................................................................................78 收款(Accepting Money)....................................................................................................90 支付方法和PayPal账号(Payment Methods and PayPal Accounts)..........................90 Google Checkout..........................................................................................................110 第五章:促销,市场营销和内容页..........................................................................................116 顾客个性化(Customer Personalization).........................................................................117 Wishlist........................................................................................................................117 产品比较(Compare Products).................................................................................118 最近浏览/比较的产品(Recently Viewed/Compared Products).............................119 新产品(New Products)............................................................................................120 RSS订阅(RSS Feeds)..............................................................................................121 层次性定价(Tier Pricing)...............................................................................................122 目录定价规则(Catalog Price Rules)...............................................................................124 规则信息(Rule Information)...................................................................................125 条件(Conditions).....................................................................................................125 动作(Actions)..........................................................................................................127 购物车定价规则(Shopping Cart Price Rules)................................................................127 规则信息(Rule Information)...................................................................................128 条件(Conditions).....................................................................................................129 动作(Actions)..........................................................................................................130 简报(Newsletter).............................................................................................................131 简报配置(Newsletter Configuration)......................................................................131 简报模板(Newsletter Templates)............................................................................132 简报队列(Newsletter Queue)..................................................................................133 简报订阅者(Newsletter Subscribers).....................................................................133 简报问题报告(Newsletter Problem Reports).........................................................134 静态区块(Static Blocks).................................................................................................134 着陆页(Landing Pages)...................................................................................................137 General Information......................................................................................................138 Custom Design.............................................................................................................138 Meta Data.....................................................................................................................139 投票(poll)........................................................................................................................140 Poll Information............................................................................................................141 Poll Answers.................................................................................................................141 搜索大纲及重定向(Search Synonyms and Re-directs)..................................................142 快速搜索(Quick Search).........................................................................................142 高级搜索(Advanced Search)..................................................................................144 第六章:客户..............................................................................................................................145 客户默认选项(Default Customer Options)....................................................................146 Account Sharing Options Share....................................................................................146 Create New Account Options.......................................................................................146 Password Options.........................................................................................................146 联系选项及Email(Contact Options and Emails)............................................................147 Editing Email Templates...............................................................................................147 Email Sender.................................................................................................................148 Contact Us....................................................................................................................148 创建新客户(Creating Customers)...................................................................................148 In Front-end..................................................................................................................149 In Admin.......................................................................................................................150 客户用户组(Customer Groups)......................................................................................151 Creating Customer Groups...........................................................................................152 Managing Customer Groups.........................................................................................152 批量更新客户(Batch Updates to Customers).................................................................153 客户账号的前台及后台功能(Front-End and Back-End functionality for Customer Accounts) 153 前台功能(Front-End)..............................................................................................154 后台功能(Back-End)..............................................................................................157 在线客户(Online Customers).........................................................................................159 第七章:报表及分析..................................................................................................................159 报表(Reports)..................................................................................................................159 销售方面的报表(Sales)..........................................................................................159 购物车报表(Shopping Cart Report).......................................................................161 产品方面的报表(Products)....................................................................................161 客户方面的报表(Customers).................................................................................162 评论报表(Review Reports).....................................................................................163 标签报表(Tags Reports).........................................................................................164 搜索词(Search Terms).............................................................................................164 Dashboard............................................................................................................................164 分析(Analytics)...............................................................................................................165 第八章:订单管理......................................................................................................................166 Overview of Order Functionality..........................................................................................166 Creating Orders.............................................................................................................166 Terminology..................................................................................................................166 Invoice.........................................................................................................................166 Managing and Editing Orders.......................................................................................167 新建订单(Creating Orders).............................................................................................167 Accessing the Order Page.............................................................................................167 Creating the Order........................................................................................................168 销售订单选项(Sales Order Options)..............................................................................171 1.发票选项(Invoice Options)..................................................................................171 2.发货选项(Shipment Options)..............................................................................174 3.信用备忘选项(Credit Memo Options)................................................................176 管理及修改订单(Managing and Editing Orders)..................................................................178 第九章:用户生成内容..............................................................................................................181 评级和评论(Ratings and Reviews)........................................................................................181 管理评级(Managing Ratings).................................................................................181 管理评论(Managing Reviews)...............................................................................183 标签(Tags).......................................................................................................................186 通过Email给朋友推荐产品(Email to a Friend Options)...............................................188 第十章:Magento升级...............................................................................................................189 使用magento connect升级Magento.....................................................................................189
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值