Magento创建后台表单处理页,假设这个模块名字为:Mage_Sintax
1. 模块声明
文件: app/etc/modules/Mage_Sintax.xml
Xml代码 收藏代码
<?xml version="1.0"?>
<config>
<modules>
<Mage_Sintax>
<active>true</active>
<codePool>local</codePool>
</Mage_Sintax>
</modules>
</config>
2. 生成Module配置文件.
文件: app/code/local/Mage/Sintax/etc/config.xml
Xml代码 收藏代码
<?xml version="1.0"?>
<config>
<modules>
<Mage_Sintax>
<version>0.1.0</version>
</Mage_Sintax>
</modules>
<global>
<models>
<sintax>
<class>Mage_Sintax_Model</class>
<resourceModel>sintax_mysql4</resourceModel>
</sintax>
<sintax_mysql4>
<class>Mage_Sintax_Model_Mysql4</class>
</sintax_mysql4>
</models>
<blocks>
<sintax>
<class>Mage_Sintax_Block</class>
</sintax>
</blocks>
<helpers>
<sintax>
<class>Mage_Sintax_Helper</class>
</sintax>
</helpers>
</global>
<admin>
<routers>
<sintax>
<use>admin</use>
<args>
<module>Mage_Sintax</module>
<frontName>sintax</frontName>
</args>
</sintax>
</routers>
</admin>
<adminhtml>
<translate>
<modules>
<mage_adminhtml>
<files>
<sintax>Mage_Sintax.csv</sintax>
</files>
</mage_adminhtml>
</modules>
</translate>
<menu>
<catalog>
<children>
<sintax_adminform translate="title" module="sintax">
<title>My Form</title>
<action>sintax/adminhtml_myform</action>
</sintax_adminform>
</children>
</catalog>
</menu>
<acl>
<resources>
<admin>
<children>
<catalog>
<children>
<sintax_adminform>
<title>My Form</title>
</sintax_adminform>
</children>
</catalog>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<sintax>
<file>sintax.xml</file>
</sintax>
</updates>
</layout>
</adminhtml>
</config>
3. 创建控制器文件 app/code/local/Mage/Sintax/controllers/Adminhtml/MyformController.php
Php代码 收藏代码
<?php
class Mage_Sintax_Adminhtml_MyformController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout()->renderLayout();
}
public function postAction()
{
$post = $this->getRequest()->getPost();
try {
if (emptyempty($post)) {
Mage::throwException($this->__('Invalid form data.'));
}
/* here's your form processing */
$message = $this->__('Your form has been submitted successfully.');
Mage::getSingleton('adminhtml/session')->addSuccess($message);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
$this->_redirect('*/*');
}
}
4. 为Form文件穿件Block类(这步是可选的)
文件名: app/code/local/Mage/Sintax/Block/Myform.php
5. Layout 文件
文件: app/design/adminhtml/default/default/layout/sintax.xml
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>
6. Form 模板页
文件: app/design/adminhtml/default/default/template/sintax/myform.phtml
Html代码 收藏代码
<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 onclick="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>
7. 翻译文件: app/locale/en_US/Mage_Sintax.csv
// this step is optional
8. Helper文件: app/code/local/Mage/Sintax/Helper/Data.php
Php代码 收藏代码
<?PHP
class Mage_Sintax_Helper_Data extends Mage_Core_Helper_Abstract {
}
?>
以下几点说明:
1. config.xml中的acl标签的含义是什么?
2. helper类是必须的,为什么?
3. 本例block声明的类型为adminhtml/template, 不必声明相应的block类
(责任编辑:好模板) |