First of all, to comply with action controller interface \Magento\Framework\App\ActionInterface::execute()
, your action must return an instance of \Magento\Framework\Controller\ResultInterface
(\Magento\Framework\App\ResponseInterface
is also supported, but is legacy and will be removed in future releases of M2, when all core usages are refactored).
So choose from available implementations of \Magento\Framework\Controller\ResultInterface
. The most suitable for custom REST API (assuming it operates with JSON) seems to be \Magento\Framework\Controller\Result\Json
. However, if you need something even more custom, consider \Magento\Framework\Controller\Result\Raw
.
Working sample:
<?php
namespace VendorName\ModuleName\Controller;
/**
* Demo of authorization error for custom REST API
*/
class RestAuthorizationDemo extends \Magento\Framework\App\Action\Action
{
/** @var \Magento\Framework\Controller\Result\JsonFactory */
protected $jsonResultFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
) {
parent::__construct($context);
$this->jsonResultFactory = $jsonResultFactory;
}
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->jsonResultFactory->create();
/** You may introduce your own constants for this custom REST API */
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_FORBIDDEN);
$result->setData(['error_message' => __('What are you doing here?')]);
return $result;
}
}
The code above will result in response with HTTP status code 403
and body {"error_message":"What are you doing here?"}
if you look at the documentation you'll find that actually the email is not optional so is needed in order to update the customer
customer-data-customer-interface {
id (integer, optional): Customer id ,
groupId (integer, optional): Group id ,
defaultBilling (string, optional): Default billing address id ,
defaultShipping (string, optional): Default shipping address id ,
confirmation (string, optional): Confirmation ,
createdAt (string, optional): Created at time ,
updatedAt (string, optional): Updated at time ,
createdIn (string, optional): Created in area ,
dob (string, optional): Date of birth ,
email (string): Email address ,
firstname (string): First name ,
lastname (string): Last name ,
middlename (string, optional): Middle name ,
prefix (string, optional): Prefix ,
suffix (string, optional): Suffix ,
gender (integer, optional): Gender ,
storeId (integer, optional): Store id ,
taxvat (string, optional): Tax Vat ,
websiteId (integer, optional): Website id ,
addresses (Array[customer-data-address-interface], optional): Customer addresses. ,
disableAutoGroupChange (integer, optional): Disable auto group change flag. ,
extensionAttributes (customer-data-customer-extension-interface, optional),
customAttributes (Array[framework-attribute-interface], optional): Custom attributes values.
}
but if you try with the email at the current release you'll see there also another param that is not optional (problably the documentation has to be updated). here is the very least needed to do the update:
{
"customer": {
"email": "email@here.me",
"firstname": "firstname new",
"lastname": "lastname new",
"website_id": 1
}
}
Best Answer
Here is an example that may help you. In this example package name is Henote and module name is Store:
step1 : webapi.xml
step2: di.xml
step3:Henote\Store\Api\StoreManagementInterface.php
step4: Henote\Store\Model\StoreManagement.php
step5:Henote\Store\Api\Data\StoreDetailsInterface.php
step6: Henote\Store\Model\StoreDetails.php