如果你已经学会了扩展模块的基本输出方法,那么本文一定是你需要的——使用正规的方法输出网页。
假设模块为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/模块名/类名/方法名