Magento使用正规的方式输出网页(使用phtml文件输出内容)

如果你已经学会了扩展模块的基本输出方法,那么本文一定是你需要的——使用正规的方法输出网页。

假设模块为Cartz_Hotel,我们想当访问
http://localhost/magento/index.php/hotel/my/room能够输出Hello , phtml Page


I. 建立controllers/MyController.php内容如下:

class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{ public function roomAction(){ $this->loadLayout(); $this->renderLayout(); } }

II. etc/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> <layout> <updates> <hotel> <file>hotel.xml</file> </hotel> </updates> </layout> </frontend> <global> <blocks> <hotel><class>Cartz_Hotel_Block</class></hotel> </blocks> </global> </config>

config.xml最重要的部分是
1. hotel.xml将稍后用来作为配置模块layout的文件
2. <class>Cartz_Hotel_Block</class>用来说明Block类命名规则(文件的目录位置)。

III. Block/Room.php

<?php class Cartz_Hotel_Block_Room extends Mage_Core_Block_Template{ public function getHello() { return "Hello, phtml page"; } }


IV. 建立phtml文件
$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<theme package>/<theme name>/template/hotel/room.phtml

<h1><?php echo $this->getHello(); ?></h1>


V. 建立Layout文件
$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<theme package>/<theme name>/layout/hotel.xml

<?xml version="1.0"?> <layout version="0.1.0"> <hotel_my_room> <reference name="root"> <action method="setTemplate"><template>page/1column.phtml</template></action> </reference> <reference name="content"> <block type="hotel/room" name="hotel_room" template="hotel/room.phtml"/> </reference> </hotel_my_room> </layout>

当访问http://localhost/magento/index.php/hotel/my/room时,Magento自动会定位到标签<hotel_my_room>对应的block。看

URL 结构 http://www.yoursite.com/index.php/模块名/类名/方法名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值