现在的位置: 首页 > 综合 > 正文

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

2018年02月07日 ⁄ 综合 ⁄ 共 2124字 ⁄ 字号 评论关闭

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

假设模块为Cartz_Hotel,我们想当访问
http://localhost/magento
/index.php
/hotel/my/room能够输出Hello , phtml Page
I. 建立controllers/MyController.php内容如下:

  1. <?php
  2. class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{
  3.     public function roomAction(){
  4.       $this->loadLayout();
  5.       $this->renderLayout();
  6.    }
  7. }

复制代码

II. etc/config.xml

  1.     <?xml version="1.0"?>
  2.     <config>
  3.        <modules>
  4.           <Cartz_Hotel>
  5.              <version>0.1.0</version>
  6.           </Cartz_Hotel>
  7.        </modules>
  8.        <frontend>
  9.           <routers>
  10.              <hotel>
  11.                 <use>standard</use>
  12.                 <args>
  13.                    <module>Cartz_Hotel</module>
  14.                    <frontName>hotel</frontName>
  15.                 </args>
  16.              </hotel>
  17.           </routers>
  18.             <layout>
  19.                 <updates>
  20.                     <hotel>
  21.                         <file>hotel.xml</file>
  22.                     </hotel>
  23.                 </updates>
  24.             </layout>
  25.        </frontend>
  26.        <global>
  27.           <blocks>
  28.              <hotel><class>Cartz_Hotel_Block
    </class></hotel>
  29.           </blocks>
  30.        </global>
  31.     </config>

复制代码

config.xml最重要的部分是

1. hotel.xml将稍后用来作为配置模块layout的

文件

2. <class>Cartz_Hotel_Block</class>用来说明Block类命名规则(文件的目录

位置

)。

III. Block/Room.php

  1.     <?php
  2.     class Cartz_Hotel_Block_Room extends Mage_Core_Block_Template{
  3.        public function getHello() {
  4.           return "Hello, phtml page";
  5.        }
  6.     }

复制代码

IV. 建立phtml文件

$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<

theme

package>/<theme name>/template/hotel/room.phtml

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

复制代码

V. 建立Layout文件

$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<theme package>/<theme name>/layout/hotel.xml

  1.     <?xml version="1.0"?>
  2.     <layout version="0.1.0">
  3.        <hotel_my_room>
  4.             <reference name="root">
  5.                 <action method="setTemplate"><template>page/1column.phtml</template></action>
  6.             </reference>
  7.             <reference name="content">
  8.                 <block type="hotel/room" name="hotel_room" template="hotel/room.phtml"/>
  9.             </reference>
  10.         </hotel_my_room>
  11.     </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文件。

抱歉!评论已关闭.