我正在尝试使用ajax来放置特定属性(制造商)的产品列表。
在我的ajax页面中我尝试使用
$this->__('hi'); //not working
错误
fatal error :在第 24 行/home/shared/nftn/NFTN/js/ajax/ajax_designer.php 中不在对象上下文中时使用 $this
mage:__('hi'); //not working
错误
fatal error :在第 23 行/home/shared/nftn/NFTN/js/ajax/ajax_designer.php 中调用未定义的方法 Mage::__()
我在页面顶部添加所需的文件
require "../../app/Mage.php";
umask(0);
Mage::app('default');
$layout = Mage::getSingleton('core/layout');
即使这些功能也不起作用
Mage::stripTags()
Mage::getLayout()
问题是什么。我怎样才能让它工作
谢谢
最佳答案
显然 $this
没有任何意义,因为您没有在对象内使用它。所有助手都有这些功能,这里我使用 'core'
因为它是最通用的,但如果您正在为自己的模块编写,那么请使用模块的助手 - 它有助于翻译。
Mage::helper('core')->__('hello');
Mage::helper('core')->stripTags('world');
getLayout()
无法从外部文件工作,因为没有与页面关联的路由器/ Controller /操作,因此没有可供使用的布局。
更长但稍微更正确的方法是使用 Magento 的 Controller 而不是外部文件。假设您的模块是 My_Module
并且 AJAX URL 是 www.example.com/mymodule/ajax/
...
app/code/local/My/Module/etc/config.xml
<config>
<frontend>
<routers>
<mymodule>
<use>standard</use>
<args>
<module>My_Module</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</frontend>
</config>
app/code/local/My/Module/controllers/AjaxController.php
<?php
class My_Module_AjaxController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->getResponse()->setBody($this->__('hi'));
}
}
有关更复杂的示例,请参阅 app/code/core/Mage/CatalogSearch/controllers/AjaxController.php
中的 Mage_CatalogSearch_AjaxController
,它使用 block 作为输出,更好地遵循MVC 范例。
关于magento - 一些magento功能在magento外部(ajax页面)无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453369/