I have created a rest web api. I want to pass multiple parameters to get my required data. How can I do that ?
interface ProductsInterface { /** * Returns greeting message to user * * @api * @param string $sku * @return string Greeting message with users name. */ public function getProductPrice($sku); /** * @param $sku * @param $attributecode * @return mixed */ public function getProductAttribute($sku, $attributecode); }
Model:
class Products implements ProductsInterface { /** * @var ProductRepositoryInterface */ private $_productRepository; /** * Product constructor. * @param ProductRepositoryInterface $productRepository */ public function __construct(ProductRepositoryInterface $productRepository) { $this->_productRepository = $productRepository; } /** * Returns greeting message to user * * @param string $sku * @return string Greeting message with users name. * @throws \Magento\Framework\Exception\NoSuchEntityException * @api */ public function getProductPrice($sku) { $prodcutBySku = $this->_productRepository->get($sku); return $prodcutBySku->getPrice(); } /** * @param $sku * @param $attributecode * @return \Magento\Framework\Api\AttributeInterface|null * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getProductAttribute($sku, $attributecode) { $prodcutBySku = $this->_productRepository->get($sku); return $prodcutBySku->getCustomAttribute($attributecode); } }
webapi.xml
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route url="/V1/products/getProductPrice/:sku" method="GET"> <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductPrice"/> <resources> <resource ref="anonymous"/> </resources> </route> <route url="/V1/products/getProductAttribute/:sku,:attr" method="GET"> <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductPrice"/> <resources> <resource ref="anonymous"/> </resources> </route>