REST API Versioning (V1 / V2 / V3) in Magento 2.3 - rest

Have clarification on implementing versioning on custom API in Magento 2.3
Current Scenario:
We have implemented custom REST API with some functionality.
API Spec:
API: https://example.domain.com/rest/default/V1/custom/module/list
Method: GET
Expected Scenario
We need to use same functionality with minor change in version 2.
API Spec
API: https://example.domain.com/rest/default/V2/custom/module/list
Method: GET
Note:
The both API end point service method should be a same.
The current scenario should not be break by handling expected scenario. Both API should be work as we expect.
Hope the below code will help for reference
/app/code/Custom/Module/etc/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/custom/module/list" method="GET">
<service class="Custom\Module\Api\StudentInterface" method="getList" />
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>
/app/code/Custom/Module/etc/di.xml
<?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="Custom\Module\Api\StudentInterface" type="Custom\Module\Service\StudentService"/>
</config>
/app/code/Custom/Module/Api/StudentInterface.php
<?php
namespace Custom\Module\Api;
interface StudentInterface
{
/**
* Get Student list
*
* #return array
*/
public function getList();
}
/app/code/Custom/Module/Service/StudentService.php
<?php
namespace Custom\Module\Service;
class StudentService
{
/**
* {#inheritdoc}
*/
public function getList()
{
//To do get Student List
}
}
Is it possible to use same Service Method and return response based on REST API Verisoning (V1 / V2 / V3) ?

Related

What Did I Do Wrong Here With Shopware 6 Product.Loaded Event

I created a bare minimum Shopware 6 plugin to display product ID when the product is loaded. It worked fine. Below is my code.
PATH: src/Resources/config/services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="TestPlugin\Listener\ProductLoadedListener" >
<tag name="kernel.event_listener" event="product.loaded" />
</service>
</services>
</container>
Below is the ProductLoadedListener.php codes
PATH: src/Listener/ProductLoadedListener.php
<?php declare(strict_types=1);
namespace TestPlugin\Listener;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
class ProductLoadedListener{
public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent){
print_r($entityLoadedEvent->getIds());
}
}
The above codes did the job it was created to do.
So I updated the ProductLoadedListener.php codes
<?php declare(strict_types=1);
namespace TestPlugin\Listener;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
class ProductLoadedListener{
public function onProductLoaded(Price $price){
print_r($price->getNet());
}
}
I go an error
Argument 1 passed to TestPlugin\Listener\ProductLoadedListener::onProductLoaded() must be an instance of Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price, instance of Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent given, called in /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php on line 270
So I am asking why I got the above error, I was expecting it to echo the net price?
Shopware will inject in the onProductLoaded function an EntityLoadedEvent object, not a Price object. That's why PHP throws this error.
If you want the get the price of the loaded product, then you should get the product from the $entityLoadedEvent and then get the price:
class ProductLoadedListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
];
}
public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent)
{
/** #var ProductCollection $loadedProducts */
$loadedProducts = $event->getEntities();
$firstProduct = $loadedProducts->first();
$productNetPrice = $firstProduct->getPrice()->first()->getNet();
dd($productNetPrice);
}
}

While Click on custom module getting an error "Invalid Form Key. Please refresh the page"

I have created a custom module whenever I click on it, I am facing the “Invalid Form Key. Please refresh the page” error. I have google regarding this error and I have found the solution to increasing the value of max_input_vars PHP configurable variable. I have increased this value but the problem is not solved.
Current PHP configurable variables are: max_input_time : 3600 max_input_vars : 200000 memory_limit : 2G
Here is my Code Hierarchy
I'm facing the error:
Controller/Adminhtml/Create/Index.php
<?php
namespace Comlitix\ComlitixInfo\Controller\Create;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Index
*/
class Index extends Action implements HttpGetActionInterface
{
const MENU_ID = 'Comlitix_ComlitixInfo::rules';
/**
* #var PageFactory
*/
protected $resultPageFactory;
/**
* Index constructor.
*
* #param Context $context
* #param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Load the page defined in view/adminhtml/layout/rules_create_index.xml
*
* #return Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu(static::MENU_ID);
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
}
}
etc/adminhtml/menu.xml
[![<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Comlitix_ComlitixInfo::comlitix" title="Comlitix" module="Comlitix_ComlitixInfo" sortOrder="30" resource="Comlitix_ComlitixInfo::comlitix"/>
<add id="Comlitix_ComlitixInfo::rules" title="Tracking Rules" module="Comlitix_ComlitixInfo" sortOrder="10" action="rules/create/index" resource="Comlitix_ComlitixInfo::rules" parent="Comlitix_ComlitixInfo::comlitix"/>
<add id="Comlitix_ComlitixInfo::reports" title="Report" module="Comlitix_ComlitixInfo" sortOrder="20" parent="Comlitix_ComlitixInfo::comlitix" action="rules/create/index" resource="Comlitix_ComlitixInfo::reports"/>
</menu>
</config>][2]][2]
etc/adminhtml/routes.xml
<?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="admin">
<route id="rules" frontName="rules">
<module name="Comlitix_ComlitixInfo"/>
</route>
</router>
</config>
view/adminhtml/layout/rules_create_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Backend\Block\Template" template="Comlitix_ComlitixInfo::rules.phtml"/>
</referenceContainer>
</body>
</page>
view/adminhtml/templates/rules.phtml
<p>Hello World!</p>
Menu.xml {action URL}
action="rules/create/index"
Anyone, please help me to fix this issue.
This type of issue mainly occurs due to URL so you have to first validate the URL of the menu
But in the current case, the controller Namespace is wrong in the file Controller/Adminhtml/Create/Index.php so this can also create the same issue
it should be
namespace Comlitix\ComlitixInfo\Controller\Adminhtml\Create;
Now we refer the URL frontname which is rules define in the file etc/adminhtml/routes.xml
Now based on that in the admin area you can change the URL in the etc/adminhtml/menu.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Comlitix_ComlitixInfo::comlitix" title="Comlitix" module="Comlitix_ComlitixInfo" sortOrder="30" resource="Comlitix_ComlitixInfo::comlitix"/>
<add id="Comlitix_ComlitixInfo::rules" title="Tracking Rules" module="Comlitix_ComlitixInfo" sortOrder="10" action="rules/create/index" resource="Comlitix_ComlitixInfo::rules" parent="Comlitix_ComlitixInfo::comlitix"/>
<add id="Comlitix_ComlitixInfo::reports" title="Report" module="Comlitix_ComlitixInfo" sortOrder="20" parent="Comlitix_ComlitixInfo::comlitix" action="rules/create/index" resource="Comlitix_ComlitixInfo::reports"/>
</menu>
</config>

Creating a admin controller using adminhtml id

I'm trying to add a controller accessible on admin menu through url:
https://dev.m2t2.com/admin_k1tgag/admin/helloWorld/index/key/0195fab99cc865bb756a77e8fe5ceedb6f8eee97de91d569398d383cef4f0d81/
Generated by the XML code inserted below. But it keeps returning
Invalid security or form key. Please refresh the page.
Router.xml :
<?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="admin">
<route id="adminhtml">
<module name="Study_Admin" before="Magento_Backend"/>
</route>
</router>
</config>
In menu i inserted:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Study_Admin::greetings" title="Greetings" translate="title" module="Study_Admin" parent="Magento_Backend::content" sortOrder="50" dependsOnModule="Study_Admin" resource="Study_Admin::greetings"/>
<add id="Study_Admin::greetings_helloworld" title="Hello World" translate="title" module="Study_Admin" parent="Study_Admin::greetings" sortOrder="10" dependsOnModule="Study_Admin" action="adminhtml/helloWorld" resource="Study_Admin::greetings"/>
</menu>
</config>
But when i access the controller through the menu i have no success. I started debugging and i checked that non-custom controllers extends \Magento\Backend\App\Action class to pass validations inside magento routing core flow. I did the same but i still have no success.
Below my controller class:
<?php
namespace Study\Controller\Adminhtml\HelloWorld;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action\Context;
use ‌Magento\Framework\App\ActionInterface;
class Index extends \Magento\Backend\App\Action
{
const MENU_Id = "Study_Admin::greetings_helloworld";
protected $resultPageFActory;
public function __construct(Context $context, PageFactory $resultPageFActory)
{
parent::__construct($context);
$this->resultPageFActory = $resultPageFActory;
}
public function execute()
{
$resultPage = $this->resultPageFActory->create();
$resultPage->setActiveMenu(static::MENU_Id);
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
// TODO: Implement execute() method.
}
}
The file structure is :
Thanx in advance, and take care.
It works... was just my namespace in Controller's class that had an error
'namespace Study\Admin\Controller\Adminhtml\Helloworld;'

How get configurable by Simple Product Magento 2 rest API

The question is simple, how can I get the Configurable Product through a Simple product through the REST API Magento 2?
I'm using the following call to get the simple product:
http://127.0.0.1/magento2/index.php/rest/V1/products/prdConfig-RED
Thank you
I have created a new module which accepts child product id as parameter and returns the parent product id and other attributes like name , thumbnail....
registeration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'WebAPI_GetParentProductThumbnail',
__DIR__
);
etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="WebAPI_GetParentProductThumbnail" setup_version="1.0.0"/>
</config>
etc/di.xml
<?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="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" type="WebAPI\GetParentProductThumbnail\Model\ChildThumbnailManagement"/>
</config>
etc/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 method="GET" url="/V1/webapi-getparentproductthumbnail/childthumbnail">
<service class="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" method="getChildThumbnail"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Api/ChildThumbnailManagementInterface.php
<?php
namespace WebAPI\GetParentProductThumbnail\Api;
interface ChildThumbnailManagementInterface
{
/**
* GET for ChildThumbnail api
* #param string $product_id
* #return string
*/
public function getChildThumbnail($product_id);
}
Model/ChildThumbnailManagement.php
<?php
namespace WebAPI\GetParentProductThumbnail\Model;
class ChildThumbnailManagement
{
/**
* {#inheritdoc}
*/
public function getChildThumbnail($product_id)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
if($product_id != ""){
//This method getParentIdsByChild($child_id) get the parent id of a configurable product.
$parent_product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product_id);
if(isset($parent_product[0]))
{
$parent_id = $parent_product[0];
//Parent object where you can get Thumbnail, name.... etc
//$parent_object = $objectManager->create('Magento\Catalog\Model\Product')->load($parent_product[0]);
echo parent_id;
}
}
return null;
}
}

Magento2 Soap service not working

I tried to create a soap service but somehow its wsdl url is not working. Below is the code:-
Api/CustomapitInterface.php
namespace W3solver\Customapi\Api;
interface CustomapiInterface
{
/**
* Returns greeting message to user
*
* #api
* #param string $name Users name.
* #return string Greeting message with users name.
*/
public function name($name);
}
model/Customapi.php
<?php
namespace W3solver\Customapi\Model;
use W3solver\Customapi\Api\CustomapiInterface;
class Customapi implements CustomapiInterface
{
/**
* Returns greeting message to user
*
* #api
* #param string $name Users name.
* #return string Greeting message with users name.
*/
public function name($name) {
return "Hello, " . $name;
}
}
etc/di.xml
<?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="W3solver\Customapi\Api\CustomapiInterface" type="W3solver\Customapi\Model\Customapi" />
</config>
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="W3solver_Customapi" setup_version="1.0.0" />
</config>
etc/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/customapi/name/:name" method="GET">
<service class="W3solver\Customapi\Api\CustomapiInterface" method="name"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
I have created this by using a refrence from http://inchoo.net/magento/api-magento/magento-2-custom-api/. I did not where this get wrong.
Below is the url i am trying to use:-
http://magento2.local/index.php/soap/default?wsdl&services=w3solverCustomapiV1
Magento 2 has API access to anonymous APIs disabled by default, you will need to enable this from the backend administration panel.
To disable this feature, log in to the Admin panel and select
Stores > Configuration > Services > Magento Web API > Web API Security.
Then select Yes from the Allow Anonymous Guest Access menu.
You can find more information on the dev guidelines here.