1

I had this issue during when I create a custom Rest API in fresh magento 2.4.4p4

enter image description here

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 Wowshop\MobileAppApi\Api;

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="Wowshop\MobileAppApi\Api\CustomerInterface" type="Wowshop\MobileAppApi\Model\Customer"/>
</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="Wowshop\MobileAppApi\Api\CustomerInterface" method="login"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

app/code/Wowshop/MobileAppApi/Model/Customer.php

<?php

namespace Wowshop\MobileAppApi\Model;

use Wowshop\MobileAppApi\Api\CustomerInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Integration\Model\Oauth\TokenFactory;

class Customer implements CustomerInterface
{
    protected $tokenModelFactory;
    protected $customerAccountManagement;
    protected $resultFactory;

    public function __construct(
        TokenFactory $tokenModelFactory,
        AccountManagementInterface $customerAccountManagement,
        ResultFactory $resultFactory,
        \Magento\Framework\Webapi\Rest\Request $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 (\Magento\Framework\Exception\LocalizedException $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

1
  • hi, I have fixed this issue, apparently its because I use redundant code such as getBodyParams, and also resultFactory, once removed its okay already Commented Jul 1 at 9:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.