1

我正在关注Magento 知识库上的 Magento 开发人员教程,在我到达第 4 部分(请参阅链接)之后,我遇到了一个问题。我已经完成了本教程这一部分中指定的更改,之后,使用“/helloworld/index/index”控制器,浏览器会显示一个完全空的输出。之前填充的“goodbye”和“params”控制器看起来不错,“showConfig”XML 输出也是如此。

任何人都可以对此有所了解吗?以下是我在第 3 部分末尾的检查点的不同之处:

diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Configviewer/Model/Observer.php
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Configviewer/Model/Observer.php    Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,32 @@
+<?php
+    class Magentotutorial_Configviewer_Model_Observer {
+        const FLAG_SHOW_CONFIG = 'showConfig';
+        const FLAG_SHOW_CONFIG_FORMAT = 'showConfigFormat';
+
+        private $request;
+
+        public function checkForConfigRequest($observer) {
+            $this->request = $observer->getEvent()->getData('front')->getRequest();
+            if($this->request->{self::FLAG_SHOW_CONFIG} === 'true'){
+                $this->setHeader();
+                $this->outputConfig();
+            }
+        }
+
+        private function setHeader() {
+            $format = isset($this->request->{self::FLAG_SHOW_CONFIG_FORMAT}) ?
+            $this->request->{self::FLAG_SHOW_CONFIG_FORMAT} : 'xml';
+            switch($format){
+                case 'text':
+                    header("Content-Type: text/plain");
+                    break;
+                default:
+                    header("Content-Type: text/xml");
+            }
+        }
+
+        private function outputConfig() {
+            die(Mage::app()->getConfig()->getNode()->asXML());
+        }
+    }
+?>
diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Configviewer/etc/config.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Configviewer/etc/config.xml    Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,20 @@
+<config>
+    <modules>
+        <Magentotutorial_Configviewer>
+            <version>0.1.0</version>
+        </Magentotutorial_Configviewer>
+    </modules>
+    <global>
+        <events>
+            <controller_front_init_routers>
+                <observers>
+                    <Magentotutorial_configviewer_model_observer>
+                        <type>singleton</type>
+                        <class>Magentotutorial_Configviewer_Model_Observer</class>
+                        <method>checkForConfigRequest</method>
+                    </Magentotutorial_configviewer_model_observer>
+                </observers>
+            </controller_front_init_routers>
+        </events>
+    </global>
+</config>
diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,23 @@
+<?php
+class Magentotutorial_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
+
+    public function indexAction() {
+
+        echo 'Hello Index!';
+
+    }
+
+    public function goodbyeAction() {
+            echo 'Goodbye World!';
+    }
+
+    public function paramsAction() {
+        echo '<dl>';
+        foreach($this->getRequest()->getParams() as $key=>$value) {
+            echo '<dt><strong>Param: </strong>'.htmlspecialchars($key).'</dt>';
+            echo '<dt><strong>Value: </strong>'.htmlspecialchars($value).'</dt>';
+        }
+        echo '</dl>';
+    }
+}
+?>
diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Helloworld/etc/config.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Helloworld/etc/config.xml  Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,18 @@
+<config>
+    <modules>
+        <Magentotutorial_Helloworld>
+            <version>0.1.0</version>
+        </Magentotutorial_Helloworld>
+    </modules>
+    <frontend>
+        <routers>
+            <helloworld>
+                <use>standard</use>
+                <args>
+                    <module>Magentotutorial_Helloworld</module>
+                    <frontName>helloworld</frontName>
+                </args>
+            </helloworld>
+        </routers>
+    </frontend>
+</config>
diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/etc/modules/Magentotutorial_Configviewer.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/etc/modules/Magentotutorial_Configviewer.xml  Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,8 @@
+<config>
+    <modules>
+        <Magentotutorial_Configviewer>
+            <active>true</active>
+            <codePool>local</codePool>
+        </Magentotutorial_Configviewer>
+    </modules>
+</config>
diff -r 98d31339093e -r fd26e7cbf555 magento/magento/app/etc/modules/Magentotutorial_Helloworld.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/etc/modules/Magentotutorial_Helloworld.xml    Thu Jul 05 09:02:36 2012 +0000
@@ -0,0 +1,8 @@
+<config>
+    <modules>
+        <Magentotutorial_Helloworld>
+            <active>true</active>
+            <codePool>local</codePool>
+        </Magentotutorial_Helloworld>
+    </modules>
+</config>

以下是导致问题的新差异:

diff -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php
--- a/magento/magento/app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php Thu Jul 05 09:02:36 2012 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php Thu Jul 05 12:13:55 2012 +0000
@@ -3,7 +3,10 @@

     public function indexAction() {

-        echo 'Hello Index!';
+        // remove our previous echo
+        // echo 'Hello Index!';
+        $this->loadLayout();
+        $this->renderLayout();

     }

diff -r fd26e7cbf555 magento/magento/app/code/local/Magentotutorial/Helloworld/simple_page.phtml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/code/local/Magentotutorial/Helloworld/simple_page.phtml   Thu Jul 05 12:13:55 2012 +0000
@@ -0,0 +1,16 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+    <title>Untitled</title>
+    <meta name="generator" content="BBEdit 9.2" />
+    <style type="text/css">
+        body {
+            background-color:#f00;
+        }
+    </style>
+</head>
+<body>
+
+</body>
+</html>
diff -r fd26e7cbf555 magento/magento/app/design/frontend/base/default/layout/local.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/magento/magento/app/design/frontend/base/default/layout/local.xml Thu Jul 05 12:13:55 2012 +0000
@@ -0,0 +1,7 @@
+<layout version="0.1.0">
+    <default>
+        <reference name="root">
+            <block type="page/html" name="root" output="toHtml" template="../../../../../code/local/Magentotutorial/Helloworld/simple_page.phtml" />
+        </reference>
+    </default>
+</layout>

提前致谢,

— 什洛米鱼

4

1 回答 1

1
<layout version="0.1.0">
    <default>
        <reference name="root">
            <block type="page/html" name="root" output="toHtml" template="../../../../../code/local/Magentotutorial/Helloworld/simple_page.phtml" />
        </reference>
    </default>
</layout>

改成:

 <layout version="0.1.0">
    <default>
        <reference name="root">
            <block type="page/html" name="root" output="toHtml" template="magentotutorial/helloworld/simple_page.phtml" />
        </reference>
    </default>
</layout>

然后将您的 simple_page.phtml 移动到:app/design/frontend/default/default/template/magentotutorial/helloworld/simple_page.phtml

那应该可以解决所有问题,我不相信您引用模板位置的方式是正确的。遵守约定总是最好的。

希望有帮助。:)

于 2012-07-10T21:54:37.750 回答