INSYNC New Logo

4 easy steps to create a Web Service in Magento 2

magento 2

Magento 2 is an open source eCommerce R&D project facilitating the improvement of Magento Core products. With the new and improved features in Magento 2, developers will be able to offer more functionality to merchants and continue to grow with the platform. Magento 2 is a prospective future version of the Magento e-commerce software, currently still in the development stage. This is the first of many posts in the next few months that will provide further insight and updates about the progress, process, technical architecture and strategy as well as many other facets that involve this important project and milestone for the Magento community.

Magento 2 development is hosted at github for Magento 2 project; which gives us all an inside peak to the evolution of the next Magento. The developer community has been complimenting their work with great suggestions and contributions on Magento 2’s GitHub repository. From there we collected the source package of present Magento 2 development.




Magento 2 will be a substantial update to the Magento platform that will include numerous rewrites and a refactoring of the codebase. The new features will be extremely compelling and the work will be worthwhile to many.

Here we will see how easily web service will develop in Magento2. Creating a module is little different than the previous one. Our main focus is to understand the changes from the previous Magento versions.

1. Create Folder Structure –

If you see the folder structure in the given image, it looks like same as the previous one but module activation part has been changed.

magento 2 - m1

Let’s create a Namespace name as Insync, under that we create a module name Shippingmethods.



Earlier to install/register we have to create a .xml file in the following way

appetcmodules[Namespace_modulename].xml

From there we can manage the main activation of module.

So in Magento2 the activation is managed by the config.xml file. Which is located under this path: appcodelocal[Namespace_modulename]etc

2. Register the Module and Activate –

Now if you see the image below for the config.xml file, you notice that the information that we use to add earlier in appetcmodules[Namespace_modulename].xml file, the same we use to add into config.xml file.

magento 2 - m2

From now on all module installation is managed in their own existing folder. This also help to install / Un installing of any third party or local module in Magento 2.

Once the above process has been completed then we can check the new install module in Admin panel. Go to the Magento Admin panel and follow the following path –

Admin-> System->Configuration->Advance.




3. Map the web service with the defined Magento Class-

Our next step is to map the module for our new web service. For this we have to create a file name api.xml in the etc folder [e.g. app/code/local/Insync/Shippingmethods/etc/api.xml], where our API calls will be defined.magento 2 - m3

You might have heard about the factory methods being removed from Magento2 and sadly it’s 100% true, calling: Mage::getModel(‘catalog/product’); It’s not going to work in Magento2, instead factory names have been replaced by full class namespaces that means now you will have to call: Mage::getModel(‘Mage_Catalog_Model_Product’);

For that we also have to write in the same way into api.xml file. Let’s take the Customer Module example –

In previous versions it’s mapped like this:

<model>customer/customer_api</model>


And now in Magento2:

<model>Mage_Customer_Model_Customer_Api</model>




But in .wsdl file there is no change, so we can carry our same knowledge. For mapping with the .wsdl file you can refer this link [http://www.magentocommerce.com/boards/viewthread/43818/]

 

4. Write the Magento code to execute the functionality –

Let’s create V2.php file in the following path

[e.g. app/code/local/Insync/Shippingmethods/Model/Shippingmethods/Api/v2.php]

Where we define the function getShippingMethods() and its body.

Now your web service is ready for use. You can see the web service definition in any browser. Type the url in the following way

http://your_magento_domain.tld/index.php/api/V2_soap?wsdl=1


Finally to check the complete functionality and feature you need to have a tool. In my suggestion, download one soapUI tool from the following link [http://sourceforge.net/projects/soapui/].

Hope this post is helpful for you to create a web services in Magento 2.

Stay tuned for more information in the weeks and months ahead, including Magento 2 release schedule, updated timetable for Community Edition alpha, beta releases, and much more!

 

Search

Search
On Key

Related Posts

APPSeCONNECT is now a completely new entity headquartered in the US! 🇺🇸