1

I create a database table 'computergames' . Now the table display in postman by get request

1 Answer 1

0

You can try the below steps to create an api to get data from your custom table

1- Create your module Visit the link to create module

2- Create a File named webapi.xml at the folder vendor>module>etc>

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <!-- get customer Api -->
    <route url="/V1/get/customer/" method="GET">
        <service class="Vendor\Module\Api\GetCustomerInterface" method="getData" />
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>

3- Create a file di.xml at vendor>module>etc> and create prefrence for your api

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Vendor\Module\Api\GetCustomerInterface" type="Vendor\Module\Model\GetCustomer" />

4- Create a File named GetCustomerInterface.php at Vendor > Module > Api

<?php
namespace Vendor\FME\Api;

interface GetCustomerInterface
{

    /**
     * GET for test api
     * @return object
     */

  public function getData();

}

5- Create your prefrence file named as GetCustomer.php at Vendor > Module > Model

<?php
namespace Vendor\Module\Model;
use Vendor\Module\Api\GetCustomerInterface;

class GetCustomer implements GetCustomerInterface
{

     /**
     * {@inheritdoc}
     */

    public function getData()
    {  
         
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('hse_mobileapp_customer');

        $select = $connection->select()
        ->from(
       ['p' => $tableName]);

       $data = $connection->fetchAll($select);

       return $data;

    }

}

Here is demo of this API running in postman

Api in postman

If you didn't understand anything or need further justification feel free to ask.

5
  • Just request GET and show the table . no any authentication Sep 1, 2022 at 5:04
  • So you can simply change the query and modify the method getData() in GetCustomer.php ( do not need to add params in method ) Sep 1, 2022 at 5:18
  • please share the code . just GetCustomer.php Sep 1, 2022 at 5:28
  • @RubelParvaz I have updated my code please try again Sep 1, 2022 at 6:04
  • 1
    Thank You so mach Sep 1, 2022 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.