Magento模块开发的简单示例
开发模式下开启日志记录,方便调试。
System->Developer
magento/var 要开启777权限,/var/log目录下能看到(exception.log,system.log)。
模块文件 (Xinson 为Packagename,News为Modulename)
Packagename_All.xml的命名表示Package中开启多个模块
/app/etc/modules/Xinson_News.xml
<?xml version="1.0"?> <config> <modules> <Xinson_News> <active>true</active> <codePool>local</codePool> </Xinson_News> </modules> </config>
在loacl新建相关目录
模块的配置文件
/app/code/local/Xinson/News/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Xinson_News> <version>0.1.0</version> </Xinson_News> </modules> </config>
添加安装文件
/app/code/local/Xinson/News/sql/news_setup/mysql4-install-0.1.0.php
<?php $installer = $this; $installer->startSetup(); $installer->run(" -- DROP TABLE IF EXISTS {$this->getTable('news')}; CREATE TABLE {$this->getTable('news')} ( `news_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL DEFAULT '', `content` text NOT NULL DEFAULT '', `created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `is_active` tinyint(1) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (`news_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); $installer->endSetup();
在config.xml配置文件,resources为执行news_setup里的文件(相关数据库操作,版本更新),translate为语言包(Xinson_News.csv放在app/locale/zh_CN里面)。
<config> <global> <resources> <news_setup> <setup> <module>Xinson_News</module> </setup> <connection> <use>core_setup</use> </connection> </news_setup> </resources> <translate> <modules> <Xinson_News> <files> <default>Xinson_News.csv</default> </files> </Xinson_News> </modules> </translate> </global> </config>
此时进入后台,清除缓存。
在system->Configuration->Mage Trash App->Extensions Installed
可以看到Xinson_News。
并且在core_resource表中能看到,xinson_setup 0.1.0 0.1.0。还有新建的表news。
在config.xml中添加资源
global表示全局,即前台,后台都能使用。adminhtml为后台,frontend为前台。
<config> <global> <resources> ...... <news_write> <connection> <use>core_write</use> </connection> </news_write> <news_read> <connection> <use>core_read</use> </connection> </news_read> </resources> </global> </config>
添加Model
/app/code/local/Xinson/News/Model/News.php
<?php class Xinson_News_Model_News extends Mage_Core_Model_Abstract { public function _construct() { parent::_construct(); $this->_init('news/news'); } }
/app/code/local/Xinson/News/Model/Mysql4/News.php
<?php class Xinson_News_Model_Mysql4_News extends Mage_Core_Model_Mysql4_Abstract { public function _construct() { $this->_init('news/news', 'news_id'); } }
/app/code/local/Xinson/News/Model/Mysql4/News/Collection.php
<?php class Xinson_News_Model_Mysql4_News_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract { public function _construct() { parent::_construct(); $this->_init('news/news'); } }
在config.xml中添加Model(在global里面)
<global> <models> <news> <class>Xinson_News_Model</class> <resourceModel>news_mysql4</resourceModel> </news> <news_mysql4> <class>Xinson_News_Model_Mysql4</class> <entities> <news> <table>news</table> </news> </entities> </news_mysql4> </models> </global>
添加Helper和Block
/app/code/local/Xinson/News/Helper/Data.php
<?php class Xinson_News_Helper_Data extends Mage_Core_Helper_Abstract { }
在config.xml中添加Helper和Block
<config> <global> <blocks> <news> <class>Xinson_News_Block</class> </news> </blocks> <helpers> <news> <class>Xinson_News_Helper</class> </news> </helpers> </global> </config>
添加样例数据和前段控制器
进入MYSQL执行
INSERT INTO `news` (`title`, `content`, `created_at`, `updated_at`, `is_active`) VALUES ('title1', 'content1', '2015-11-29 08:00:00', '2015-11-29 08:00:00', 1); INSERT INTO `news` (`title`, `content`, `created_at`, `updated_at`, `is_active`) VALUES ('title2', 'content2', '2015-11-29 08:10:00', '2015-11-29 08:10:00', 1);
前端控制器
/app/code/local/Xinson/News/controllers/IndexController.php
<?php class Xinson_News_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $news = Mage::getModel('news/news')->getCollection(); foreach ($news as $new) { var_dump($new->getData()); } } }
config.xml路由配置
<config> <frontend> <routers> <news> <use>standard</use> <args> <module>Xinson_News</module> <frontName>news</frontName> </args> </news> </routers> </frontend> </config>
在浏览器中打开 http://localhost/news 将看到var_dump的数据
此时的config配置文件
<?xml version="1.0"?> <config> <modules> <Xinson_News> <version>0.1.0</version> </Xinson_News> </modules> <global> <resources> <news_setup> <setup> <module>Xinson_News</module> </setup> <connection> <use>core_setup</use> </connection> </news_setup> <news_write> <connection> <use>core_write</use> </connection> </news_write> <news_read> <connection> <use>core_read</use> </connection> </news_read> </resources> <models> <news> <class>Xinson_News_Model</class> <resourceModel>news_mysql4</resourceModel> </news> <news_mysql4> <class>Xinson_News_Model_Mysql4</class> <entities> <news> <table>news</table> </news> </entities> </news_mysql4> </models> <translate> <modules> <Xinson_News> <files> <default>Xinson_News.csv</default> </files> </Xinson_News> </modules> </translate> <blocks> <news> <class>Xinson_News_Block</class> </news> </blocks> <helpers> <news> <class>Xinson_News_Helper</class> </news> </helpers> </global> <frontend> <routers> <news> <use>standard</use> <args> <module>Xinson_News</module> <frontName>news</frontName> </args> </news> </routers> </frontend> </config>