最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > Magento > Magento教程 >

Magento创建后台表单处理页

时间:2014-08-22 02:09来源:未知 作者:最模板zuimoban 点击:
假设这个模块名字为: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. 生
假设这个模块名字为:Mage_Sintax 

1. 模块声明 

文件: app/etc/modules/Mage_Sintax.xml 
Xml代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <config>  
  3.     <modules>  
  4.         <Mage_Sintax>  
  5.             <active>true</active>  
  6.             <codePool>local</codePool>  
  7.         </Mage_Sintax>  
  8.     </modules>  
  9. </config>  


2. 生成Module配置文件. 
文件: app/code/local/Mage/Sintax/etc/config.xml 
Xml代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <config>  
  3.     <modules>  
  4.         <Mage_Sintax>  
  5.             <version>0.1.0</version>  
  6.         </Mage_Sintax>  
  7.     </modules>  
  8.      
  9.     <global>  
  10.         <models>  
  11.             <sintax>  
  12.                 <class>Mage_Sintax_Model</class>  
  13.                 <resourceModel>sintax_mysql4</resourceModel>  
  14.             </sintax>  
  15.             <sintax_mysql4>  
  16.                 <class>Mage_Sintax_Model_Mysql4</class>  
  17.             </sintax_mysql4>  
  18.         </models>  
  19.         <blocks>  
  20.             <sintax>  
  21.                 <class>Mage_Sintax_Block</class>  
  22.             </sintax>  
  23.         </blocks>  
  24.         <helpers>  
  25.             <sintax>  
  26.                 <class>Mage_Sintax_Helper</class>  
  27.             </sintax>   
  28.         </helpers>  
  29.     </global>  
  30.      
  31.     <admin>  
  32.         <routers>  
  33.             <sintax>  
  34.                 <use>admin</use>  
  35.                 <args>  
  36.                     <module>Mage_Sintax</module>  
  37.                     <frontName>sintax</frontName>  
  38.                 </args>  
  39.             </sintax>  
  40.         </routers>  
  41.     </admin>  
  42.      
  43.     <adminhtml>  
  44.         <translate>  
  45.             <modules>  
  46.                 <mage_adminhtml>  
  47.                     <files>  
  48.                         <sintax>Mage_Sintax.csv</sintax>  
  49.                     </files>  
  50.                 </mage_adminhtml>  
  51.             </modules>  
  52.         </translate>  
  53.          
  54.         <menu>  
  55.             <catalog>  
  56.                 <children>  
  57.                     <sintax_adminform translate="title" module="sintax">  
  58.                         <title>My Form</title>  
  59.                         <action>sintax/adminhtml_myform</action>  
  60.                     </sintax_adminform>  
  61.                 </children>  
  62.             </catalog>  
  63.         </menu>  
  64.          
  65.         <acl>  
  66.             <resources>  
  67.                 <admin>  
  68.                     <children>  
  69.                         <catalog>  
  70.                             <children>  
  71.                                 <sintax_adminform>  
  72.                                     <title>My Form</title>  
  73.                                 </sintax_adminform>  
  74.                             </children>  
  75.                         </catalog>  
  76.                     </children>  
  77.                 </admin>  
  78.             </resources>  
  79.         </acl>  
  80.          
  81.         <layout>  
  82.             <updates>  
  83.                 <sintax>  
  84.                     <file>sintax.xml</file>  
  85.                 </sintax>  
  86.             </updates>  
  87.         </layout>  
  88.     </adminhtml>  
  89. </config>  


3. 创建控制器文件 app/code/local/Mage/Sintax/controllers/Adminhtml/MyformController.php 
Php代码  收藏代码
  1. <?php  
  2. class Mage_Sintax_Adminhtml_MyformController extends Mage_Adminhtml_Controller_Action  
  3. {  
  4.     public function indexAction()  
  5.     {  
  6.         $this->loadLayout()->renderLayout();  
  7.     }  
  8.      
  9.     public function postAction()  
  10.     {  
  11.         $post = $this->getRequest()->getPost();  
  12.         try {  
  13.             if (emptyempty($post)) {  
  14.                 Mage::throwException($this->__('Invalid form data.'));  
  15.             }  
  16.              
  17.             /* here's your form processing */  
  18.              
  19.             $message = $this->__('Your form has been submitted successfully.');  
  20.             Mage::getSingleton('adminhtml/session')->addSuccess($message);  
  21.         } catch (Exception $e) {  
  22.             Mage::getSingleton('adminhtml/session')->addError($e->getMessage());  
  23.         }  
  24.         $this->_redirect('*/*');  
  25.     }  
  26. }  




4. 为Form文件穿件Block类(这步是可选的) 

文件名: app/code/local/Mage/Sintax/Block/Myform.php 

5. Layout 文件 

文件: app/design/adminhtml/default/default/layout/sintax.xml 
Xml代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <layout>  
  3.     <sintax_adminhtml_myform_index>  
  4.         <reference name="content">  
  5.             <block type="adminhtml/template" name="myform" template="sintax/myform.phtml"/>  
  6.         </reference>  
  7.     </sintax_adminhtml_myform_index>  
  8. </layout>  



6. Form 模板页 
文件: app/design/adminhtml/default/default/template/sintax/myform.phtml 
Html代码  收藏代码
  1. <div class="content-header">  
  2.     <table cellspacing="0" class="grid-header">  
  3.         <tr>  
  4.             <td><h3><?php echo $this->__('My form title')?></h3></td>  
  5.             <td class="a-right">  
  6.                 <button onclick="editForm.submit()" class="scalable save" type="button"><span>Submit my form</span></button>  
  7.             </td>  
  8.         </tr>  
  9.     </table>  
  10. </div>  
  11. <div class="entry-edit">  
  12.     <form id="edit_form" name="edit_form" method="post" action="<?php echo $this->getUrl('*/*/post')?>">  
  13.         <h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('This fieldset name')?></h4>  
  14.         <fieldset id="my-fieldset">  
  15.             <table cellspacing="0" class="form-list">  
  16.                 <tr>  
  17.                     <td class="label"><?php echo $this->__('Field label')?> <span class="required">*</span></td>  
  18.                     <td class="input-ele"><input class="input-text required-entry" name="myform[myfield]" /></td>  
  19.                 </tr>  
  20.             </table>  
  21.         </fieldset>  
  22.     </form>  
  23. </div>  
  24. <script type="text/javascript">  
  25.     var editForm = new varienForm('edit_form');  
  26. </script>  



7. 翻译文件: app/locale/en_US/Mage_Sintax.csv 

// this step is optional 

8. Helper文件: app/code/local/Mage/Sintax/Helper/Data.php 
Php代码  收藏代码
  1. <?PHP  
  2. class Mage_Sintax_Helper_Data extends Mage_Core_Helper_Abstract {  
  3. }  
  4. ?>  


以下几点说明: 
1. config.xml中的acl标签的含义是什么? 
2. helper类是必须的,为什么? 
3. 本例block声明的类型为adminhtml/template, 不必声明相应的block类  (责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容