我正在运行 Magento 1.7.0.2。在我的派生控制器类中,我有一个包含以下代码的操作:
公共函数 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();
}
我只是想从 Alan Storm 的书 No Frills Magento Layout 中以字符串形式传递的一些 XML 加载布局,但出现以下错误。我究竟做错了什么?
如果这是从嵌入式 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}