如果你已经学会了扩展模块
的基本输出
方法,那么本文一定是你需要的——使用正规的方法输出网页
。
假设模块为Cartz_Hotel,我们想当访问
http://localhost/magento
/index.php
/hotel/my/room能够输出Hello , phtml Page
I. 建立controllers/MyController.php内容如下:
- <?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。看
<block type="hotel/room" name="hotel_room" template="hotel/room.phtml"/>
block的type是hotel/room,将执行对应Cartz_Hotel_Block_Room类,然后找到对应的template/hotel/room.phtml文件。