1

I'd like to setup Magento so that if I make a request with an URL such as: http://mymagento.com/api/storeconfig (actual url format not important)

would return json with the data requested. How can I achieve this? Here's how I'm retrieving the data:

public function getStoreSettings()
{
    //this successfully retrieves the value of my custom settings property
    $my_property= Mage::getStoreConfig('my_module/my_settings/my_property', 'default');

    $data = array('my_property'=>$my_property);
    $json = Mage::helper('core')->jsonEncode($data);

    //return json here
}

Where would the right place to add this code? How can I map an URL to call this function and return the response as json? I appreciate if anyone could point me in the right direction!

1

After lots of playing around with Magento, trying to figure out all the magic going on, I found a solution to my problem, and am adding a solution for those who may come across the same task.

1 - Go to your app/etc/modules/YourPackage_YourModule.xml This is what I have:

<config>
  <modules>
    <YourPackage_YourModule>
      <active>true</active>
      <codePool>community</codePool>
    </YourPackage_YourModule>
  </modules>

  <frontend>
        <routers>
            <yourmodule>
                <use>standard</use>
                <args>
                    <module>YourPackage_YourModule</module>
                    <frontName>yourmodule</frontName>
                </args>
            </yourmodule>
        </routers>
    </frontend>
</config>

this will create a route for http://yourmagento.com/yourmodule/yourcontroller/youraction

2- Go to app/code/community/YourPackage/YourModule/controllers/ Create your YourController.php. This is mine:

<?php

class YourPackage_YourModule_YourController extends Mage_Core_Controller_Front_Action
{

    public function indexAction()
    { 
        $response = array('my_key'=> 'my_value');

        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
    }
}

?>

There's a lot of magic going on with the naming conventions, for a more in-depth walk-through, check out Magento for Developers: Part 3 - Magento Controller Dispatch

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.