我放了一个小模块,它将XML页面结构写入文件var/log/
夹中的日志文件。
app / code / CustomerParadigm / PageXml / etc / module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="CustomerParadigm_PageXml" setup_version="1.0.0" schema_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_Theme"/>
</sequence>
</module>
</config>
app / code / CustomerParadigm / PageXml / etc / events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_generate_blocks_after">
<observer name="customerparadigm_layout_generate_blocks_after" instance="CustomerParadigm\PageXml\Model\Layout" />
</event>
</config>
app / code / CustomerParadigm / PageXml / registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'CustomerParadigm_PageXml',
__DIR__
);
app / code / CustomerParadigm / PageXml / Model / Layout.php
<?php
namespace CustomerParadigm\PageXml\Model;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Layout implements ObserverInterface {
protected $_logger;
public function __construct ( \Psr\Log\LoggerInterface $logger ) {
$this->_logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$log_file = BP . '/var/log/page_layout.xml';
if (file_exists($log_file)) {
unlink($log_file);
}
$xml = $observer->getEvent()->getLayout()->getXmlString();
/*$this->_logger->debug($xml);*/
$writer = new \Zend\Log\Writer\Stream($log_file);
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($xml);
return $this;
}
}
这里没有什么令人惊奇的事情,而且我敢肯定还有很多改进的空间(使用内置记录器是其中之一),但是当我开发站点时,它可以为我完成工作。
catalog.topnav
。我在base主题模块的default.xml中找到它。