创建Magento模块
由于我在做我自己的Magento项目,我将使用我自己的项目名“App”。 然后,我们要创建以下目录结构
app/code/local/App/Shopping/Block
app/code/local/App/Shopping/Controller //controllers基类
app/code/local/App/Shopping/controllers
app/code/local/App/Shopping/etc
app/code/local/App/Shopping/Helper
app/code/local/App/Shopping/Model
app/code/local/App/Shopping/sql
class App_Shopping_Controller_Action extends Mage_Core_Controller_Front_Action{
}
你的插件并不一定需要包含以上所有的目录,但是为了以后开发方便,我们还是在一开始就把目录创建好。接下来我们要创建两个文件,一个是config.xml,放在etc目录下面
app/code/local/App/Shopping/etc/config.xml
文件内容如下
<config>
<modules>
<App_Shopping>
<version>0.1.0</version>
</App_Shopping>
</modules>
</config>
第二个文件需要在如下位置创建
app/etc/modules/App_Shopping.xml
第二个文件应该遵循如下命名规则“Packagename_Modulename.xml”,文件内容如下
<config>
<modules>
<App_Shopping>
<active>true</active>
<codePool>local</codePool>
</App_Shopping>
</modules>
</config>
该文件的目的是让Magento系统载入该模块。<active>标签为true表示使该模块生效。
也可以是Packagename_All.xml,里面配置所有 Module。
<?xml version="1.0"?>
<config>
<modules>
<App_Catalog>
<active>true</active>
<codePool>local</codePool>
</App_Catalog>
<App_Shopping>
<active>true</active>
<codePool>local</codePool>
</App_Shopping>
</modules>
</config>
我们先不管这些文件是干什么的,以后会解释。建立好这两个文件以后,你的模块的骨架就已经完成了。Magento已经知道你的模块存在,但是现在你的模块不会做任何事情。我们来确认一下Magento确实装载了你的模块
- 清空Magento缓存
- 在后台管理界面,进入 System->Configuration->Advanced
- 展开“Disable Modules Output”
- 确认“App_shopping ”显示出来了
如果你看到“App_ shopping ”,那么恭喜你,你已经成功创建了你第一个Magento模块!
2创建的模块不会做任何事情,下面我们来为这个模块加入逻辑
首先在App下创建新的模块,依次创建如下文件:
/app/code/local/App/Shopping/controllers/CartController.php
<?php
class App_Shopping_CartController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
echo 'Hello Magento';
//显示layout中配置的block Shopping_Cart_index
$this->loadLayout();
$this->renderLayout();
}
}
编辑/app/code/local/App/Shopping/etc/config.xml文件,加入如下代码:
<?xml version="1.0"?>
<config>
<modules>
<app_shopping>
<version>0.1.0</version>
</app_shopping>
</modules>
<frontend>
<routers>
<app_shopping>
<use>standard</use>
<args>
<module>App_Shopping</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>shopping</frontName>
</args>
</app_shopping>
</routers>
<layout> <!-- 不配置layout标签默认读customer.xml-->
<updates>
<app_shopping>
<file>shopping.xml</file>
</app_shopping>
</updates>
</layout>
</frontend>
</config>
frontend/routers/用来设置使该模块从前端显示的入口。frontName稍后将出现在url中 /shopping/cart
修改视图文件app/design/frontend/[myinterface]/[mytheme]/layout/shopping.xml在layout标签中,添加下面内容:
<ticket_index_index><!-- frontname_controller_action -->
<update handle="customer_account"/>
<reference name="my.account.wrapper">
<block type="ticket/ticket" name="ticket" template="ticket/index.phtml"/><!-- 使用block文件 -->
</reference>
</ticket_index_index>
注意,XML的大小写敏感。 后台管理 System->Configuration->设计->主题 缺省。 来生效设置
添加后台的layout xml需要在config.xml添加adminhtml节点
<adminhtml>
<layout>
<updates>
<sintax>
<file>sintax.xml</file>
</sintax>
</updates>
</layout>
</adminhtml>
文件: app/design/adminhtml/default/default/layout/sintax.xml
<?xml version="1.0"?>
<layout>
<sintax_adminhtml_myform_index>
<reference name="content">
<block type="adminhtml/template" name="myform" template="sintax/myform.phtml"/>
</reference>
</sintax_adminhtml_myform_index>
</layout>
Form 模板页
文件: app/design/adminhtml/default/default/template/sintax/myform.phtml
<div class="content-header">
<table cellspacing="0" class="grid-header">
<tr>
<td><h3><?php echo $this->__('My form title')?></h3></td>
<td class="a-right">
<button οnclick="editForm.submit()" class="scalable save" type="button"><span>Submit my form</span></button>
</td>
</tr>
</table>
</div>
<div class="entry-edit">
<form id="edit_form" name="edit_form" method="post" action="<?php echo $this->getUrl('*/*/post')?>">
<h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('This fieldset name')?></h4>
<fieldset id="my-fieldset">
<table cellspacing="0" class="form-list">
<tr>
<td class="label"><?php echo $this->__('Field label')?> <span class="required">*</span></td>
<td class="input-ele"><input class="input-text required-entry" name="myform[myfield]" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
<script type="text/javascript">
var editForm = new varienForm('edit_form');
</script>
自定义模块开发完后,需要程序自动创建mysql脚本http://hudeyong926.iteye.com/blog/1670398