How to create JSON controller in Magento 2
Creating a JSON controller in Magento 2 to handle AJAX requests and return JSON responses is a breeze. With these straightforward steps, you can confidently create a robust JSON controller that perfectly suits your needs.
Step 1: Create a Custom Module. (if you haven't already)
You need to create a custom module in Magento 2 if you haven't already. You can do this by following these steps:
- Create a directory for your module under
app/code(e.g.,app/code/Devhooks/YourModule). - Inside your module directory, create the necessary module registration files, like
registration.phpandetc/module.xml. - Enable your module by running the following command.
saveCopyzoom_out_mapphp bin/magento module:enable Devhooks_YourModule php bin/magento setup:upgrade
Step 2: Create a Controller File.
Inside your module directory, create a controller file in the following path: app/code/Devhooks/YourModule/Controller/Path/To/YourController.php
Here's an example of how your controller file might look:
saveCopyzoom_out_map<?php
namespace Devhooks\YourModule\Controller\Path\To;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\JsonFactory;
class YourController implements HttpGetActionInterface
{
/**
* @var JsonFactory
*/
private $jsonFactory;
/**
* JsonResponse constructor.
*
* @param JsonFactory $jsonFactory
*/
public function __construct(JsonFactory $jsonFactory)
{
$this->jsonFactory = $jsonFactory;
}
/**
* Execute action and return JSON response.
*/
public function execute()
{
$data = ['message' => 'Hello, Devhooks JSON Sample Text!'];
$resultJson = $this->jsonFactory->create();
$resultJson->setData($data);
return $resultJson;
}
}
In this example, we've created a controller that returns a JSON response with a simple message.
Step 3: Configure the Route
You need to configure a route for your controller in your module's routes.xml file, which should be located at app/code/Devhooks/YourModule/etc/frontend/routes.xml. If it doesn't exist, create it.
Here's an example of how your routes.xml might look:
saveCopyzoom_out_map<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="yourmodule" frontName="yourmodule">
<module name="Devhooks_YourModule" before="Magento_Customer" />
</route>
</router>
</config>
This code sets up a route with the front name "yourmodule" and associates it with your custom module.
Step 4: Access Your JSON Controller
You can access your JSON controller by making an AJAX request to the URL that corresponds to your controller. In this example, the URL would be something like http://yourmagento.com/yourmodule/path/to/yourcontroller.
That's it! You've now created a JSON controller in Magento 2 that can handle AJAX requests and return JSON responses. We hope this article helped you to know How to create JSON controller in Magento 2.
Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
AI-Powered Recommended Articles
Magento 2 JSON encode or decode
Understand how to use JSON encode and decode functions in Magento 2 for effective API data handling and encoding.
How to Use Event in Magento 2
Learn how to use events in Magento 2 to trigger custom functionality and extend store operations.
How to Create a Custom Console Command in Magento 2
Learn how to create custom console commands in Magento 2 to extend the command line interface for your specific needs.
How to set customer data in httpContext and retrieve it in Magento 2.
Learn how to set and retrieve customer data using HttpContext in Magento 2 for personalized customer experiences.
How to Insert Multiple Records using ResourceConnection in Magento 2
Learn how to insert multiple records efficiently in Magento 2 using resource connection to manage database operations effectively.
How to Create UI Component Form and CRUD Operation In Magento 2
Learn how to create UI component forms and implement CRUD operations in Magento 2 for efficient backend management.