gpt4 book ai didi

magento - 从观察者覆盖整个magento布局

转载 作者:行者123 更新时间:2023-12-02 21:18:32 25 4
gpt4 key购买 nike

问题空间

我正在尝试使用 magento 中的观察者根据请求中的参数完全替换给定请求的整个布局。

我面临的问题是,Magento 仍在尝试加载与我在观察者中指定的根 block 模板不同的根 block 模板(特别是产品页面的“frontend/base/default/template/page/1column.phtml”)这是后端设计选项卡中配置的模板使用的默认根 block 模板)。因为它没有使用我指定的布局,所以它会在 PHP 尝试加载主题中不存在的模板时死掉。

感谢任何指导。

注意:我没有使用 CMS 页面来测试这个概念,因为它们加载后端指定的自己的模板。我创建了一个测试产品并正在使用其产品页面。通过请求以下 URL 进行测试:http://mymagentosite.com/test-product?ajax=1

可能的问题

  • 我可能没有监听正确的事件来完全替换布局。文档很少,所以我根据其他 Stack Overflow 对布局问题的回答进行猜测。
  • 布局元素的文件夹结构对我来说就像巫术,因为我发现关于该主题的不同意见(例如 page.xml 与 local.xml)

实现

我创建了一个带有观察者和非常小的布局的模块,如下所示:

模块文件夹结构

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

config.xml

<!-- 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>

观察者.php

<!-- 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');
}
}
}

本地.xml

<!-- 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>

测试.phtml

<!-- 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>

MyCompany_MyModule.xml

<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyCompany_MyModule>
</modules>
</config>

最佳答案

这是我构建的一个可行的解决方案,它确实可以通过 controller_front_init_before 事件切换设计包/主题。

当然,您可以检查 changeConfigNode() 方法中的一些请求参数Mage::app()->getRequest()->getParam('switch') 并只需附加请求参数 switch/1 即可触发更改。

   <global>
<events>
<controller_front_init_before>
<observers>
<cartware_change_the_node>
<class>cartware_guest2customer/observer</class>
<method>changeConfigNode</method>
</cartware_change_the_node>
</observers>
</controller_front_init_before>
</events>
</global>

观察者方法:

public function changeConfigNode(){

/** left for testing
Mage::log( Mage::app()->getConfig()->getXmlString());
**/

Mage::app()->getConfig()->setNode('stores/default/design/package/name', 'enterprise');
Mage::app()->getConfig()->setNode('stores/default/design/package/theme', 'enterprise');
Mage::app()->getConfig()->setNode('stores/default/design/package/default_theme', 'enterprise');
Mage::app()->getConfig()->setNode('stores/default/design/theme/default', 'enterprise');
Mage::app()->getConfig()->setNode('stores/default/design/theme/layout', 'enterprise');
Mage::app()->getConfig()->setNode('stores/default/design/theme/template', 'enterprise');



}

它没有额外的检查,但它可能会帮助你!祝你好运!

关于magento - 从观察者覆盖整个magento布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com