Cor*_*ren 6 layout magento magento-1.7
我正在尝试使用magento中的观察者来根据请求中的参数完全替换给定请求的整个布局.
我面临的问题是,Magento仍在尝试加载与我在Observer中指定的模板不同的根块模板(特别是"frontend/base/default/template/page/1column.phtml",用于产品页面,在后端的设计选项卡中配置的模板使用的默认根块模板.因为它没有使用我指定的布局,所以它在PHP中死于尝试加载主题中不存在的模板.
任何指导表示赞赏.
注意:我没有使用CMS页面来测试这个概念,因为它们加载了后端指定的模板.我创建了一个测试产品并使用了它的产品页面.通过请求以下URL进行测试:http://mymagentosite.com/test-product?ajax=1
我创建了一个带有观察者和非常小的布局的模块,如下所示:
app
??? code
? ??? local
? ??? MyCompany
? ??? MyModule
? ??? etc
? ? ??? config.xml
? ??? Model
? ??? Observer.php
??? design
? ??? frontend
? ??? myTheme
? ??? default
? ??? layout
? ? ??? local.xml
? ??? template
? ??? test.phtml
??? etc
??? modules
??? MyCompany_MyModule.xml
Run Code Online (Sandbox Code Playgroud)
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_MyModule>
<version>0.1.0.0</version>
</MyCompany_MyModule>
</modules>
<global>
<events>
<controller_action_layout_generate_xml_before>
<observers>
<myCompany_myModule_model_observer>
<type>singleton</type>
<class>MyCompany_MyModule_Model_Observer</class>
<method>changeRequestLayout</method>
</myCompany_myModule_model_observer>
</observers>
</controller_action_layout_generate_xml_before>
</events>
</global>
</config>
Run Code Online (Sandbox Code Playgroud)
<!-- language: lang-php -->
<?php
class MyCompany_MyModule_Model_Observer
{
public function changeRequestLayout($observer)
{
if ($observer->getAction()->getRequest()->isAjax()) {
Mage::getDesign()->setArea('frontend')->setPackageName('myTheme')->setTheme('default');
}
}
}
Run Code Online (Sandbox Code Playgroud)
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0.0">
<default>
<block type="page/html" name="root" output="toHtml" template="test.phtml" />
</default>
</layout>
Run Code Online (Sandbox Code Playgroud)
<!-- language: lang-html -->
<!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>Testing, testing...1...2...3...</title>
<style type="text/css">
body {
background-color:#f00;
}
</style>
</head>
<body>
<h1>This is a test of the Magento layout system. This is only a test. If this were not a test, real content would follow.</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyCompany_MyModule>
</modules>
</config>
Run Code Online (Sandbox Code Playgroud)
Magento 的分层 XML 布局系统是完成一个微不足道的用例时造成混乱的根源。
由于基本/默认布局 XML 与我指定的任何布局相结合,因此我不仅必须覆盖根布局,还必须覆盖在 base/default/page.xml 中定义的所有其他派生布局。这是不切实际的,因为 Magento 根据客户端正在浏览的页面类型定义了大量自定义布局。
最干净的解决方案是在 template/page 目录中创建符号链接,以指向以下所有布局的 test.phtml 文件。这保证了 Magento 将只使用我的布局模板。
注意:您必须启用模板符号链接才能使其工作(在管理后端,导航至系统>配置>高级>开发人员,展开“模板设置”并将“允许符号链接”设置为“是”)。