magento 模块开发

2011-07-22 11:30:00

步骤 I: 建立模块目录及文件结构骨架。
目录结构

【Magento】
app
- etc/
- modules/
Cartz_Hotel.xml
- code/
- local/
- Cartz/
- Hotel/
- controllers
MyController.php
- etc
config.xml

步骤 II: Cartz_Hotel.xml

<config>
<modules>
<Cartz_Hotel>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Cartz_Hotel>
</modules>
</config>

该文件的目的是让Magento系统载入该模块。<active>标签为true表示使该模块生效。注意标<Cartz_Hotel>是命名约定,从而约束了你的代码应该放在app/code/Cartz/Hotel目录下。

步骤 III: MyController.php

<?php
class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{
public function helloAction() {
echo "My First Module";
}
}

类 名构成: 前缀(Cartz_Hotel)加上文件的名字(IndexController),然后要求扩展基类Mage_Core_Controller_Front_Action。稍后我们要看看如何调用indexAction方法来输出“My FirstModule”.

步骤 IV: config.xml

<?xml version="1.0"?>
<config>
<modules>
<Cartz_Hotel>
<version>0.1.0</version>
</Cartz_Hotel>
</modules>
<frontend>
<routers>
<hotel>
<use>standard</use>
<args>
<module>Cartz_Hotel</module>
<frontName>hotel</frontName>
</args>
</hotel>
</routers>
</frontend>
</config>frontend/routers/用来设置使该模块从前端显示的入口。

假设Magento在本机安装的访问首页是http://localhost/magento/index.php, 那么在浏览器地址栏中输入

http://localhost/magento/index.php/hotel/my/hello

将在页面输入"My First Module"

从而得出Magento模块url访问的命名规范

http://

<host>/<Magento虚拟目录>/<config.xm中的frontName>/<Controller文件名去掉Controller>/<Controller文件的方法名去掉Action>

 

 

 

Magento-在扩展的模块中使用模型(Model)

在已经建立好Cartz_Hotel的模块目录结构的基础上:

1.在etc/config.xml的根标签(config)下增加下面的代码

<global>
<models>
<hotel>
<class>Cartz_Hotel_Model</class>
</hotel>
</models>

</global>

2.增加文件model/Room.php<?php
class Cartz_Hotel_Model_Room extends Mage_Core_Model_Abstract
{
protected function _construct(){
}

public function hello(){
echo "Hello Model";
}

}
?>

3.建立一个controller文件(controllers/RoomController.php)调用上面模型类的hello方法<?php
class Cartz_Hotel_RoomController extends Mage_Core_Controller_Front_Action{

public function loadAction($id = null){
$roomObj = Mage::getModel('hotel/room');
$roomObj->hello();
}
}?>

当在浏览器地址栏中输入:

http://localhost/magento/index.php/hotel/room/load

你这是应该看到页面上输出字符串: Hello Model.

关键代码:

Mage::getModel('hotel/room')

用来获得Model类的实例

 

 

Magento_在Module中访问数据库

假设你已经在完成了系列文章中前面的部分。那么已经有了一个模型文件为model/Room.php,并且etc/config.xml文件的global内容如下:

<global>
<models>
<hotel>
<class>Cartz_Hotel_Model</class>
</hotel>
</models>
</global>

I. 现在修改etc/config.xml为

<global>
<models>
<hotel>
<class>Cartz_Hotel_Model</class>
<resourceModel>hotel_mysql4</resourceModel>
</hotel>
<hotel_mysql4>
<class>Cartz_Hotel_Model_Mysql4</class>
<entities>
<room>
<table>rooms</table>
</room>
</entities>
</hotel_mysql4>
</models>
<resources>
<hotel_setup>
<setup>
<module>Cartz_Hotel</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</hotel_setup>
<hotel_write>
<connection>
<use>core_write</use>
</connection>
</hotel_write>
<hotel_read>
<connection>
<use>core_read</use>
</connection>
</hotel_read>
</resources>
</global>

II. 创建表结构和录入测试数据

create table rooms(id int not null auto_increment, name varchar(100), primary key(id));

 

insert into rooms values(1,'Royal Room');

insert into rooms values(2,'Standard Room');

III. model/Room.php内容修改如下:

<?php
class Cartz_Hotel_Model_Room extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('hotel/room');
}
}
?>IV. 新建文件model/Mysql4/Room.php <?php
class Cartz_Hotel_Model_Mysql4_Room extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('hotel/room', 'id');
}
}
?>V. 修改controllers/RoomController.php. <?php
class Cartz_Hotel_RoomController extends Mage_Core_Controller_Front_Action{

public function loadAction($id = null){
$id= (int) $this->getRequest()->getParam('id', false);
$roomObj = Mage::getModel('hotel/room');

$room = $roomObj->load($id);
$name = $room->getName();
if(empty($name)) {
echo "Not Existed Room.";
} else {
echo "Room Name : ".$name;
}
}
}
?>

当在浏览器地址栏中输入:
http://localhost/magento/index.php/hotel/room/load/id/1

你这是应该看到页面上输出字符串:
Room Name : Royal Room

这里还揭示了页面参数是如何传给Magento的Controller里的:
url上的id/1在action方法中用
$this->getRequest()->getParam('id', false);
来获取.

 

 

Magento - 如何调用SQL语句

I. 创建表结构和测试数据

 

create table rooms(id int not null auto_increment, name varchar(100), primary key(id));

 

insert into rooms values(1,'Royal Room');

insert into rooms values(2,'Standard Room');

II.创建controllers/RoomController.php:

<?php
class Cartz_Hotel_RoomController extends Mage_Core_Controller_Front_Action{
public function listingAction() {
$handle = Mage::getSingleton('core/resource')->getConnection('core_write');

$query = $handle->query('select name from rooms');
while ($row = $query->fetch()) {
$row = new Varien_Object($row);
echo "<strong>" . $row->getName() . "</strong><br/>";
}

}
}?>

在地址栏中输入: http://localhost/magento /index.php/hotel/room/listing , 页面将输出:

Royal Room
Standard Room

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注