branko ajzele, senior developer / project manager

Posts / Articles

How to create Magento REST server extension

I just wrote an article on How to create Magento AMF server extension over at Inchoo.net so I figured why not squeeze another one here on ActiveCodeline.com. If there is anything I like about Zend its service components. Creating a REST, SOAP or AMF server in Zend is a breeze. Since Magento is built on top of Zend, the job of creating a simple REST server extension is really simple.

For your module to act as a basic REST server you need only 3 files. MyCompany_MyModule.xml that goes under the /app/etc/modules/ folder. Then IndexController.php that goes under the /app/code/local/MyCompany/MyModule/controllers/IndexController.php. And last but not least the config.xml file that goes under the /app/code/local/MyCompany/MyModule/etc/ folder.

Inside your IndexController.php file you will actualy hold the code for your REST server (Zend_Rest_Server).

Here is an example of indexAction from my IndexController.php file:

 
...
public function indexAction()
{   
	$server = new Zend_Rest_Server();
	$server->setClass('My_Service_Class');
	$server->handle();
}
...

The above code is the “server”. Now we need to make all of this accessible via Url, so we can access that server. We need to add entry to config.xml file. Below is the full example of my config.xml file.

 
<?xml version="1.0"?>
 
<config>
    <modules>
        <MyCompany_MyModule>
            <version>1.0.0</version>
        </MyCompany_MyModule>
    </modules>
 
	<frontend>
		<routers>
			<mymodule>
				<use>standard</use>
				<args>
				<module>MyCompany_MyModule</module>
				<frontName>myrestserver</frontName>
			</args>
			</mymodule>
		</routers>
	</frontend>    
</config>

Above piece of code sets access to REST server on Url like http://server/store/index.php/myrestserver.

Thats it.

View Comments

  1. QB3RT /

    so does this mean that the API is now accessable via REST using the same method calls as before?

  2. QB3RT /

    I am currently writing some product import functionality via SOAP which is slow per request compared to REST so this would help alot. I guess the best way is to just dive in and try it! :) nice tutorials btw, a few from you have really helped with my understanding of magento’s inner workings :)

  3. This is really cool. I'm stuck on one part though. On the line with setClass, it is definitely finding my class but when I try to use any of the functions with ?method=functionName, it says the method's unavailable.

  4. Jaesin /

    Thanks for the post. I like how the zend server works. But in config.xml, how do you tell mage to load your class file that handles the rest request? and where should I put that file? I may seem obvious to some but for those who are new to dealing with Magento, it's not.

    It is worth mentioning that your rest handler class needs needs to extend Zend_Controller_Request_Abstract .

  5. Mahednra Dandage /

    Hi All,

    When i have implemented this code, but i got an error like –
    Fatal error: Call to undefined method InterfaceName_ModuleName_IndexController::hasAction() in appcodecoreMageCoreControllerVarienRouterStandard.php on line 209

    Please suggest

    Thanks

  6. Mahendra Dandage /

    Hi All,

    When i have implemented this code, but i got an error like –
    Fatal error: Call to undefined method InterfaceName_ModuleName_IndexController::hasAction() in appcodecoreMageCoreControllerVarienRouterStandard.php on line 209

    Please suggest

    Thanks

blog comments powered by Disqus