Magento :: 模块中的布局
是否可以在模块内创建布局文件?如何 ?
目的:
我想为产品添加某种统计命中计数器,并且我不想覆盖产品类,因为我正在使用的某些模块已经完成了这一点。因此,我认为最好有一个自定义模块,其中包含一个由布局语句调用的块。 当然,我可以轻松编辑我的私有 local.xml 或更改主题布局文件夹中的另一个布局 xml,但我希望此功能在所有主题中可用(独立于任何选定的主题)。
一些限制:
- 所有代码都在一个模块中
- ...以便它与主题无关
- ...以便该模块可以与其他人共享,而无需他们更改任何内容(例如主题文件),以便安装/加载我的模块就足够了
我也会接受不同的统计命中计数器加载方法(使用相同的约束)
Is it possible to create a layout file inside of a module ? How ?
For what:
I want to add a some kind of statistics hit counter for products, and I don't want to override the products class, as that is already done by some module I'm using. Thus I thought it would be best to have a custom module with a block that would be called by a layout statement.
Of course I could easily edit my private local.xml or make changes to another layout-xml in the layout folder of my theme, but I want this feature to be available in all themes (independent of any selected theme).
Some constraints:
- All code in one single module
- ... so that it is theme independent
- ... so that the module can be shared with others without them having to change anything (like theme files), so that the install/load of my module would be enough
I would also accept different approaches for my statistics hit counter loading (using the same constraints)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。只需在以下路径中创建布局 xml 文件:/design/frontend/default/default/layout/yourlayout.xml(或任何主题名称),并在模块 etc/config.xml 中添加适当的语句:
此示例适用于前端用户,但 adminhtml 布局可以以类似的方式更新。如果某些功能不起作用,请务必检查您的布局是否位于正确的主题/包目录中。
编辑:
第二种方法:
您可以使用自己的控制器,它将扩展核心功能(目录控制器之一) - 只需重写它(或只是产品视图)行动)。在其操作方法中添加如下内容:
第三种方法:
与前一种类似,但您可以使用一些事件观察器,并尝试
Mage::getSingleton('core/layout')< /code>,并在那里注入你的块。并非在所有情况下布局都可用(尝试 post_dispatch 系列)。
我真的不推荐第二种和第三种方法,因为如果其他人想找到这个“神奇”块的来源,它肯定会查找 int app/design/(...) 目录。在控制器或模型中找到它可能非常棘手...
如果您不想显示统计计数器,您还可以使用事件(如 post_dispatch)来计算控制器调度。只需创建一个附加到它的观察者,并将数据存储在数据库中。
Yes it is possible. Just create your layout xml file in the following path: /design/frontend/default/default/layout/yourlayout.xml(or whatever your theme name is), and add a proper statement in your modules etc/config.xml:
This sample is for frontend user, but adminhtml layouts can be updated in a similar manner. If something doesn't work, be sure to check if your layout is in the proper theme/package directory.
Edit:
Second approach:
You can use a controller of your own, which will extend the core functionality (one of the catalog controllers) - just rewrite it (or just product view action). Inside its action method add something like this:
Third approach:
Similar to the previous one, but you can use some event observer, and try
Mage::getSingleton('core/layout')
, and inject your block there. Not in all events the layout will be already available (try the post_dispatch family).I don't really recommend the second and third approach, because if someone else wants to find where this 'magic' block comes from, it will most surely look int app/design/(...) directory. Finding it in your controller or model, may be very tricky...
If you don't want to display your statistic counter, you can also use events (like post_dispatch) to count the controller dispatches. Just create an observer attached to it, and store your data in the DB.