Creating a New Magento REST API for Category Listings
Hello developers! Today we’re going to tackle an essential task in Magento development: creating a custom REST API endpoint to fetch category listings. This guide will take you step-by-step from setting up your environment to deploying your new API.
Prerequisites
Before we dive in, make sure you have the following:
- A local or remote Magento installation running.
- Composer installed and configured.
- Basic knowledge of Magento’s architecture and REST APIs.
Step 1: Enable REST API
First, ensure that Magento’s default REST API is enabled. Open your app/etc/local.xml file and find this line in the <global> section:
<api>
<resources>
<!-- ... -->
<admin>
<title>Admin: Admin</title>
<sort_order>10</sort_order>
<resource>
<id>default, admin, api_access</id>
</resource>
</admin>
<!-- ... -->
</resources>
</api>
Ensure the api_access resource is included. If not, add it as shown above.
Step 2: Create the API Controller
Navigate to your Magento root directory and run:
bin/magento setup:upgrade
Next, create a new REST API controller file at app/code/YourVendor/CategoryApi/etc/adminhtml.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Adminhtml:etc/adminhtml.xsd">
<menu>
<add id="YourVendor_CategoryApi::categoryapi"
title="Category API"
parent="Magento_Backend::content"
sortOrder="10" />
</menu>
</config>
Replace YourVendor with your actual vendor name.
Now, create the API controller file at app/code/YourVendor/CategoryApi/Controller/Index/Category.php:
namespace YourVendor\CategoryApi\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Category implements HttpGetActionInterface
{
protected $pageFactory;
public function __construct(
Context $context,
PageFactory $pageFactory
) {
$this->pageFactory = $pageFactory;
}
public function execute()
{
return $this->pageFactory->create();
}
}
Step 3: Set Up the Route
Open app/code/YourVendor/CategoryApi/etc/adminhtml/di.xml and add this route configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Route\ConfigInterface">
<arguments>
<argument name="routes" xsi:type="array">
<item name="categoryapi" xsi:type="array">
<item name="frontName" xsi:type="string">categoryapi</item>
<item name="default" xsi:type="string">index_index</item>
<item name="id" xsi:type="string">YourVendor_CategoryApi</item>
</item>
</argument>
</arguments>
</type>
</config>
Step 4: Fetch Category Listings
To fetch category listings, you’ll need to load the Magento\Catalog\Model\Category collection. Update your app/code/YourVendor/CategoryApi/Controller/Index/Category.php file:
public function execute()
{
$categoryCollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
$categories = $categoryCollection->load()->getItems();
return $this->_jsonFactory->create()->setData($categories)->sendResponse();
}
Step 5: Deploy and Test
Deploy your new module:
bin/magento setup:upgrade
bin/magento cache:flush
Now, access your Magento installation at http://yourdomain.com/categoryapi to see the category listing API endpoint in action.
Remember to configure security settings for your API endpoints according to Magento’s official documentation.
That’s it! You now have a custom Magento REST API for fetching category listings. For more advanced use cases, consider checking out the Laravel Company blog for tips on optimizing performance and enhancing security.
Happy coding!