Magento的后台的模块开发相对于Magento的前台模块开发而言,要复杂一些,这里,说下这次示例的Magento后台模块要实现的功能,其实很简单,就是前台有个表单提交数据,然后在后台展示提交的数据,实现前后台的一个交互。
首先来做一些准备工作:
1.安装好Magento
2.使用mysql工具
3.打开Symlinks:
System > Configuration > Developer > Template Settings,设置为Yes.
4.关掉缓存:
System > Cache Management > Select All > Disable > Submit,点击Flush Magento Cache.
5.打开错误报告:
找到 index.php 取消下面这行的注释:
ini_set('display_errors', 1);
然后将下面代码:
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
}
修改为:
//if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
//}
6.打开错误日志:
System > Configuration > Developer,选择 Log Settings,设置为Enabled.
7.修改seesion过期时间,
对于开发人员来说,免于不停的登录麻烦:(magento默认是15分钟)
System > Configuration->System > Advanced > Admin,选择Security,默认是900seconds,
开发过程中,设置为86400seconds(24小时)。
此外,你还需要设置php.ini的maxlifetime session expiration time,修改如下:
session.gc_maxlifetime 86400
8.可以为magento在虚拟机创建一个快照,利于备份和还原
9.先创建一个前台模块
对于前台模块如何创建,Magento的执行流程是怎样的,这里就不再累述,直接贴代码:
创建目录结构
app
|-code
|-----local
|----------Message
|--------------Count
|------------------Block
|---Adminhtml
|------------------controllers
|---Adminhtml
|------------------etc
|----config.xml
|------------------Helper
|------------------Model
跟前台模块不同的是,在Block和controllers下分别多了Adminhtml文件夹。
首先还是,需要magento加载模块,Message_Count.xml
etc/modules/Message_Count.xml
<?xml version="1.0"?>
<config>
<modules>
<Message_Count>
<version>0.1.0</version>
</Message_Count>
</modules>
<frontend><!--这里是前台部分-->
<routers>
<count>
<use>standard</use>
<args>
<module>Message_Count</module>
<frontName>count</frontName>
</args>
</count>
</routers>
<layout>
<updates>
<count>
<file>count.xml</file>
</count>
</updates>
</layout>
</frontend>
<!--这里是后台部分-->
<admin>
<routers>
<count>
<use>admin</use>
<args>
<module>Message_Count</module>
<frontName>count</frontName>
</args>
</count>
</routers>
</admin>
<adminhtml>
<menu>
<catalog>
<children>
<Count translate="title" module="count">
<title>Count</title>
<sort_order>180</sort_order>
<action>count/adminhtml_count</action>
</Count>
</children>
</catalog>
</menu>
<acl><!--这里是权限-->
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<Message_Count>
<title>count Module</title>
<sort_order>10</sort_order>
</Message_Count>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<count>
<file>count.xml</file>
</count>
</updates>
</layout>
</adminhtml>
<global>
<models><!--创建Model-->
<count>
<class>Message_Count_Model</class>
<resourceModel>count_mysql4</resourceModel>
</count>
<count_mysql4>
<class>Message_Count_Model_Mysql4</class>
<entities>
<count>
<table>count</table>
</count>
</entities>
</count_mysql4>
</models>
<resources>
<count_setup>
<setup>
<module>Message_Count</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</count_setup>
<count_write>
<connection>
<use>core_write</use>
</connection>
</count_write>
<count_read>
<connection>
<use>core_read</use>
</connection>
</count_read>
</resources>
<blocks><!--创建Block-->
<count>
<class>Message_Count_Block</class>
</count>
</blocks>
<helpers><!--创建helper-->
<count>
<class>Message_Count_Helper</class>
</count>
</helpers>
</global>
</config>
现在来写控制器IndexController.php
<?php
class Message_Count_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->_initLayoutMessages('core/session');
$this->renderLayout();
}
}
在控制器IndexController.php中,有$this->loadLayout();$this->renderLayout();即需要加载模板文件template下的phtml文件。这里我们先来给数据库添加一张表,这里使用sql文件件:
sql
|----count_setup
|-------mysql4-install-0.1.0.php
mysql4-install-0.1.0.php代码如下:
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('count')};
CREATE TABLE {$this->getTable('count')} (
`count_id` int(11) unsigned primary key auto_increment not null,
`Model_Name` varchar(200) not null,
`Name` varchar(250) not null,
`Serial_Number` varchar(100) not null,
`Email` varchar(50) not null,
`Date_Purchased` date,
`Messages_Source` varchar(30),
`Content` varchar(300) not null
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
创建了数据表,Magento需要与数据库有CURD操作,就必须会涉及到资源模型,所有,接下来,继续来Model文件夹下来内容:
Model
|----Count.php
|----Mysql4
|---Count.php
|----Count
|----Collection.php
Model/Count.php
<?php
class Message_Count_Model_Count extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('count/count');
}
}
Model/Mysql4/Count.php
<?php
class Message_Count_Model_Mysql4_Count extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
// Note that the count_id refers to the key field in your database table.
$this->_init('count/count', 'count_id');
}
}
Model/Mysql4/Count/Collection.php
<?php
class Message_Count_Model_Mysql4_Count_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('count/count');
}
}
到此为止,Model就差不多写完了。但是还有很多工作要做,
Block和Helper的内容要怎么写呢?
Helper/Data.php
<?php
class Message_Count_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Block/Count.php
<?php
class Message_Count_Block_Count extends Mage_Core_Block_Template
{
}
现在只剩下,模板phtml文件和布局文件layout下的xml文件了。
design/frontend/rwd/default/layout/count.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
</default>
<count_index_index>
<reference name="content">
<block type="count/count" name="count" template="count/count.phtml" />
</reference>
</count_index_index>
<count_index_save>
<reference name="content">
<block type="count/count" name="count" template="count/save.phtml" />
</reference>
</count_index_save>
</layout>
design/frontend/rwd/default/template/count/count.phtml
<form id="count" action="<?php echo Mage::getUrl('count/index/save') ?>" method="post" >
<fieldset>
<ul>
<li>
<label><?php echo $this->__('Model Name') ?></label>
</li>
<li>
<input type="text" id="Model_Name" name="Model_Name" class="required-entry"/>
</li>
<li>
<label><?php echo $this->__('Name') ?></label></li>
<li>
<input type="text" id="Name" name="Name" class="required-entry"/>
</li>
<li>
<label><?php echo $this->__('Serial Number') ?></label>
</li>
<li>
<input type="text" id="Serial_Number" name="Serial_Number" class="validate-number required-entry"/>
</li>
<li>
<label><?php echo $this->__('Email') ?></label>
</li>
<li>
<input type="text" id="Email" name="Email" class="required-entry validate-email"/>
</li>
<li>
<label><?php echo $this->__('Data Purchased') ?></label>
</li>
<li>
<input type="date" id="Data_Purchased" name="Data_Purchased" class="validate-date" />
</li>
<li>
<label for="Message_Source"><?php echo $this->__('How Did You Hear About INGEAR?') ?></label>
</li>
<li>
<select id="Messages_Source" name="Messages_Source" class="validate-select">
<option value="Web_search">Web search</option>
<option value="Online_review">Online review</option>
<option value="Online_Ad">Online Ad</option>
<option value="Other">Other</option>
</select>
</li>
<li>
<label><?php echo $this->__('Let us know how we\'re doing!Tell us what you think about our products?') ?></label>
</li>
<li>
<textarea rows="10" id="Content" name="Content"></textarea>
</li>
<li>
<input type="submit" value="<?php echo $this->__('sub') ?>" />
</li>
</ul>
</fieldset>
</form>
现在可以来看看效果:
然后再在IndexController.php控制器中添加模板提交的处理方法
public function saveAction()
{
$data = $this->getRequest()->getPost();
$model = Mage::getModel('count/count');
$result = $model->setData($data)->save();
//$this->loadLayout();
//$this->renderLayout();
$this->_redirect('*/*/');
}
接下来,要做的就是写后台部分,实现表单提交,后台接收和展示。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。