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.php and etc/module.xml.
  • Enable your module by running the following command.
saveCopyzoom_out_map
php 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.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off