本文参考Branko Ajzele所著Getting Started with Magento Extension Development一书,示例中将采用和书中相同的命名空间(Namespace)Foggyline以及插件名(Module Name)Happyhour,Magento安装在http://localhost/magento文件夹下。
第一步新建配置文件app/etc/modules/Foggyline_HappyHour.xml,输入如下内容
<?xml version="1.0"?> <config> <modules> <Foggyline_HappyHour> <active>true</active> <codePool>community</codePool> </Foggyline_HappyHour> </modules> </config>
此时进入后台访问System > Configuration > Advanced > Disable Modules Output,即可发现Foggyline_HappyHour模块已加入到后台中了(注意:下拉框中的Disable仅仅是针对输出而言,要取消应用插件应修改代配置文件active标签中的true为false)
第二步新建插件配置文件app/code/community/Foggyline/HappyHour/etc/config.xml并输入:
<?xml version="1.0"?> <config> <modules> <Foggyline_Happyhour> <version>1.0.0.0</version> </Foggyline_Happyhour> </modules> </config>
仅仅通过上面的配置文件对Magento不会产生任何影响,下面我们添加一个控制器在前台输出Hello World,实现这一输出需要在frontend中定义routers,添加完成后的config.xml内容如下:
<?xml version="1.0"?> <config> <modules> <Foggyline_HappyHour> <version>1.0.0.0</version> </Foggyline_HappyHour> </modules> <frontend> <routers> <foggyline_happyhour> <use>standard</use> <args> <module>Foggyline_HappyHour</module> <frontName>happyhour</frontName> </args> </foggyline_happyhour> </routers> </frontend> </config>
Magento中采用http://example.com/
frontName/actionControllerName/actionMethod/来解析router,通过以上对frontName的定义,访问路径将会是http://example.com/happyhour/*。接下来创建app/code/community/Foggyline/HappyHour/controllers/HelloController.php类文件,添加内容如下:
<?php class Foggyline_HappyHour_HelloController extends Mage_Core_Controller_Front_Action { public function helloWorldAction() { echo 'Hello World #1.'; } }
访问http://localhost/magento/index.php/happyhour/hello/helloWorld将会在页面中输出 Hello World #1.
接下来我们在config.xml的config标签内添加blocks元素
<global> <blocks> <foggyline_happyhour> <class>Foggyline_HappyHour_Block</class> </foggyline_happyhour> </blocks> </global>
以上foggyline_happyhour称之为类群(class group),我们将原先helloWorldAction()方法的代码修改如下:
<?php class Foggyline_HappyHour_HelloController extends Mage_Core_Controller_Front_Action { public function helloWorldAction() { //echo 'Hello World #1.'; $this->loadLayout(); $block = $this->getLayout()->createBlock('foggyline_happyhour/hello'); $block->setText("Hello World #2."); $this->getLayout()->getBlock('content')->insert($block); $this->renderLayout(); } }
以上代码将会调用app/code/community/Foggyline/HappyHour/Block/Hello.php文件,原因在于Magento使用了autoload机制,这也是为什么我们在创建插件时需要严格遵循Magento的命名规则。通常Magento的类可以分四个部分:vendor namespace, extension name, class group(主要有Model, Block, Helper)和filename,这四个部分决定去哪个位置加载文件,即VendorNamespace/ModuleName/ClassGroup/FileName.php.
根据上述路径创建Hello.php,并添加如下内容:
<?php class Foggyline_Happyhour_Block_Hello extends Mage_Core_Block_Text { }
此时再访问http://localhost/magento/index.php/happyhour/hello/helloWorld会在页面中打印出Hello World #2.以上代码中Hello block继承了Mage_Core_Block_Text类,实际应用中我们通常会继承Mage_Core_Block_Template类,这时Hello.php将会是类似下面的内容:
<?php class Foggyline_Happyhour_Block_Hello extends Mage_Core_Block_Template { public function __construct(){ parent::__construct(); $this->setTemplate('foggyline_happyhour/hello.phtml'); } }
Mage_Core_Block_Text和Mage_Core_Block_Template的区别在于后者要求在theme文件夹下定义一个视图文件*.phtml。这就要求创建一个app/design/frontend/default/default/template/foggyline_happyhour/hello.phtml文件或app/design/frontend/base/default/template/foggyline_happyhour/hello.phtml文件,后者更为安全一样,因为根据Magento的fallback机制在当前主题中不包含所要调用的文件时会去base目录下对应位置调取。在hello.phtml中输入Hello World #3.访问http://localhost/magento/index.php/happyhour/hello/helloWorld将会在页面中打印出Hello World #3.
为进一步了解模型、资源和集合,我们对config.xml文件做更进一步的修改
<?xml version="1.0"?> <config> <modules> <Foggyline_HappyHour> <version>1.0.0.0</version> </Foggyline_HappyHour> </modules> <frontend> <routers> <foggyline_happyhour> <use>standard</use> <args> <module>Foggyline_HappyHour</module> <frontName>happyhour</frontName> </args> </foggyline_happyhour> </routers> </frontend> <global> <blocks> <foggyline_happyhour> <class>Foggyline_HappyHour_Block</class> </foggyline_happyhour> </blocks> <models> <foggyline_happyhour> <class>Foggyline_HappyHour_Model</class> <resourceModel>foggyline_happyhour_resource</resourceModel> </foggyline_happyhour> <foggyline_happyhour_resource> <class>Foggyline_HappyHour_Model_Resource</class> <entities> <user> <table>foggyline_happyhour_user</table> </user> </entities> </foggyline_happyhour_resource> </models> <resources> <foggyline_happyhour_setup> <setup> <model>Foggyline_HappyHour</model> </setup> </foggyline_happyhour_setup> </resources> </global> </config>
原书配套示例代码下载地址:http://www.packtpub.com/code_download/14239