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
0.1.0
standard
Message_Count
count
count.xml
admin
Message_Count
count
180
count/adminhtml_count
Allow Everythingcount Module10
count.xml
Message_Count_Model
count_mysql4
Message_Count_Model_Mysql4
Message_Count
core_setup
core_write
core_read
Message_Count_Block
Message_Count_Helper
现在来写控制器IndexController.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代码如下:
$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
class Message_Count_Model_Count extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('count/count');
}
}
Model/Mysql4/Count.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
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
class Message_Count_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Block/Count.php
class Message_Count_Block_Count extends Mage_Core_Block_Template
{
}
现在只剩下,模板phtml文件和布局文件layout下的xml文件了。
design/frontend/rwd/default/layout/count.xml
design/frontend/rwd/default/template/count/count.phtml
<?php echo $this->__('Model Name') ?>
<?php echo $this->__('Name') ?>
<?php echo $this->__('Serial Number') ?>
<?php echo $this->__('Email') ?>
<?php echo $this->__('Data Purchased') ?>
<?php echo $this->__('How Did You Hear About INGEAR?') ?>
Web search
Online review
Online Ad
Other
<?php echo $this->__('Let us know how we\'re doing!Tell us what you think about our products?') ?>
现在可以来看看效果:
然后再在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('*/*/');
}
接下来,要做的就是写后台部分,实现表单提交,后台接收和展示。