I create a database table 'computergames' . Now the table display in postman by get request
1 Answer
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
If you didn't understand anything or need further justification feel free to ask.
- Just request GET and show the table . no any authenticationRubel Parvaz– Rubel Parvaz2022-09-01 05:04:37 +00:00Commented 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 )Syed Hassan Zamir– Syed Hassan Zamir2022-09-01 05:18:26 +00:00Commented Sep 1, 2022 at 5:18
- please share the code . just GetCustomer.phpRubel Parvaz– Rubel Parvaz2022-09-01 05:28:05 +00:00Commented Sep 1, 2022 at 5:28
- @RubelParvaz I have updated my code please try againSyed Hassan Zamir– Syed Hassan Zamir2022-09-01 06:04:34 +00:00Commented Sep 1, 2022 at 6:04
