开发者

Some magento functions are not working in outside magento(ajax page)

开发者 https://www.devze.com 2023-04-05 07:01 出处:网络
I am trying to use ajax to place the product list from particular attribute(manufacturer). In my ajax page i try to use

I am trying to use ajax to place the product list from particular attribute(manufacturer).

In my ajax page i try to use

$this->__('hi'); //not working

error

Fatal error: Using $this when not in object context in /home/shared/nftn/NFTN/js/ajax/ajax_designer.php on line 24

mage:__('hi'); //not working

error

Fatal error: Call to undefined method Mage::__() in /home/shared/nftn/开发者_JAVA百科NFTN/js/ajax/ajax_designer.php on line 23

i add required files in the top of the page

require "../../app/Mage.php";
umask(0);
Mage::app('default');
$layout = Mage::getSingleton('core/layout');

Even these functions also not working

Mage::stripTags()

Mage::getLayout()

What was the problem.How can i make it work

Thanks


Obviously $this has no meaning because you're not using it from within an object. All helpers have those functions, here I use 'core' because it is the most generic but if you're writing for your own module then use your module's helper - it helps with translation.

Mage::helper('core')->__('hello');
Mage::helper('core')->stripTags('world');

getLayout() cannot work from an external file because there is no router/controller/action associated with the page, hence no layout to use.


The longer but slightly more correct way would be to work with Magento's controllers rather than external files. Say your module is My_Module and the AJAX URL is 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'));
    }
}

For a more complex example see Mage_CatalogSearch_AjaxController in app/code/core/Mage/CatalogSearch/controllers/AjaxController.php, it uses blocks as it's output which better follows the MVC paradigm.

0

精彩评论

暂无评论...
验证码 换一张
取 消