I had this issue during when I create a custom Rest API in fresh magento 2.4.4p4
As you can see the error I get is Class string does not exist
Below are my code folder structure, which I found to be correct already
app/code/Wowshop/MobileAppApi/
├── Api/
│ └── CustomerInterface.php
├── etc/
│ ├── di.xml
│ ├── webapi.xml
│ └── module.xml
├── Model/
│ └── Customer.php
└── registration.php
Please find below content of the 6 file here
app/code/Wowshop/MobileAppApi/Api/CustomerInterface.php
<?php
namespace WowshopMobileAppApiApi;
interface CustomerInterface
{
/**
* POST for login API
* @param string $username
* @param string $password
* @return string
*/
public function login($username,$password);
}
app/code/Wowshop/MobileAppApi/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="WowshopMobileAppApiApiCustomerInterface" type="WowshopMobileAppApiModelCustomer"/>
</config>
app/code/Wowshop/MobileAppApi/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="Wowshop_MobileAppApi" setup_version="1.0.0"/>
</config>
app/code/Wowshop/MobileAppApi/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/mobile/customer/login" method="POST">
<service class="WowshopMobileAppApiApiCustomerInterface" method="login"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app/code/Wowshop/MobileAppApi/Model/Customer.php
<?php
namespace WowshopMobileAppApiModel;
use WowshopMobileAppApiApiCustomerInterface;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkControllerResultFactory;
use MagentoCustomerApiAccountManagementInterface;
use MagentoIntegrationModelOauthTokenFactory;
class Customer implements CustomerInterface
{
protected $tokenModelFactory;
protected $customerAccountManagement;
protected $resultFactory;
public function __construct(
TokenFactory $tokenModelFactory,
AccountManagementInterface $customerAccountManagement,
ResultFactory $resultFactory,
MagentoFrameworkWebapiRestRequest $request
) {
$this->tokenModelFactory = $tokenModelFactory;
$this->customerAccountManagement = $customerAccountManagement;
$this->resultFactory = $resultFactory;
$this->request = $request;
}
/**
* Accept username and password and return token string
* @author : Farhan
*
* @api
* @param string $username
* @param string $password
* @return string
*/
public function login($username, $password)
{
$username = $this->request->getBodyParams('username');
$password = $this->request->getBodyParams('password');
try {
$customer = $customerAccountManagement->authenticate($username, $password);
$token = $tokenModelFactory->create();
$token->createCustomerToken($customer->getId());
$accessToken = $token->getToken();
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData(['access_token' => $accessToken]);
return $result;
} catch (MagentoFrameworkExceptionLocalizedException $e) {
// Handle login failure
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData(['message' => $e->getMessage()]);
return $result;
} catch (Exception $e) {
// Handle unexpected exceptions
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData(['message' => 'An error occurred.']);
return $result;
}
}
}
I am unable to figure out how to resolve this isssue, from my checking the docblock I put already correct and same as per the CustomerInterface.php
and Customer.php
I have tried changing the @param string $username $password
in both file to @param string[] $username $password or @param mixed $username $password
but none helps as it gives out other error
If request
* @api
* @param string[] $username
* @param string[] $password
* @return string[]
Error
"The "string" value's type is invalid. The "string[]" type was expected. Verify and try again."
If request
* @api
* @param mixed $username
* @param mixed $password
* @return string
Error
"message": "Class string does not exist",
Please help me understand how this error occur and how to fix this