【发布时间】:2014-03-08 10:45:12
【问题描述】:
我从Magento for Developers指南开始学习magento。所以我用他们的名字创建模块。但是当我尝试http://works.dev/magento/?showConfig=true时它显示以下错误。
XML Parsing Error: XML or text declaration not at start of entity
Location: http://works.dev/magento/?showConfig=true
Line Number 1, Column 2: <?xml version="1.0"?>
-^
(works.dev 是我的本地主机域。magento 路径工作正常。)
这是我的配置文件。
app/code/local/Magentotutorial/Configviewer/etc/config.xml
<?xml version="1.0"?>
<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>
app/etc/modules/Magentotutorial_Configviewer.xml
<?xml version="1.0"?>
<config>
<modules>
<Magentotutorial_Configviewer>
<active>true</active>
<codePool>local</codePool>
</Magentotutorial_Configviewer>
</modules>
</config>
app/code/local/Magentotutorial/Configviewer/Model/Observer.php
<?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());
}
}
我确实清除了缓存并检查了日志。我无法从那些中找到问题。
【问题讨论】:
-
hi 应该是 app/code/local/Magentotutorial/Configviewer/etc/config.xml 中的第一行,xml 标签之前不应有空格
-
第 1 行第 1 列一定有一些错误的字符,因为错误消息指出
<?xml version从第 2 列开始。我猜可能是编码问题。保存文件时使用了哪种字符编码?或者可能是 BOM 问题。如果您的编辑器自动将 BOM 放置在文件的开头,请将其停用,保存文件并重试。 -
声明前可能有很多空白。
-
我在整个项目中用空格搜索
-
已尝试删除观察者文件的
<?php标记前面的空格?这也可能是一个原因。