Alan Storm No Frills Magento布局:从内存加载XML文件报告错误

时间:2013-06-17 12:36:29

标签: php magento simplexml magento-1.7

我正在运行Magento 1.7.0.2。 在我的派生控制器类中,我有一个包含以下代码的操作:

public function indexAction(){

$layout = Mage::getSingleton('core/layout');
$xml = simplexml_load_string('<layout>
  <block type="core/text" name="root" output="toHtml">
    <action method="setText"><text>Hello!</text></action>
  </block>
</layout>');
$layout->setXml($xml);
$layout->generateBlocks();
echo $layout->getOutput();

}

我只是尝试从一些XML传递的布局加载一个字符串,如Alan Storm的书No Frills Magento Layout,但是得到以下错误。我做错了什么?

如果这是从嵌入式XML以编程方式加载XML布局文件以进行测试和摆弄的正确方法,为什么我的SimpleXMLElement不会被强制转换为VarienSimplexmlElement?

Recoverable Error: Argument 1 passed to Varien_Simplexml_Config::setXml() must be an instance of Varien_Simplexml_Element, instance of SimpleXMLElement given, called in /var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/controllers/IndexController.php on line 17 and defined  in /var/www/magpractice/lib/Varien/Simplexml/Config.php on line 133

#0 /var/www/magpractice/lib/Varien/Simplexml/Config.php(133): mageCoreErrorHandler(4096, 'Argument 1 pass...', '/var/www/magpra...', 133, Array)
#1 /var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/controllers/IndexController.php(17): Varien_Simplexml_Config->setXml(Object(SimpleXMLElement))
#2 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Alanstormdotcom_Helloworld_IndexController->indexAction()
#3 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#4 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#5 /var/www/magpractice/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#6 /var/www/magpractice/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#7 /var/www/magpractice/index.php(88): Mage::run('', 'store')
#8 {main}

1 个答案:

答案 0 :(得分:3)

原谅我的愚蠢。我忘了将'Mage_Core_Model_Layout_Element'添加为simplexml_load_string()的第二个参数。