Why laravel returns class not exist error? However everything seems fine

Viewed 241

I created service in the app/services folder and I'm using it controller. In the services folder I have the following.

namespace App\Services;

use GuzzleHttp\Client;

class SubscriptionService
{
    private $subUsername = 'M****************b';
    private $subPassword = 'V********g';
    private $subSource = 'o*********k';
    private $subMinisite = 'a*******m';

    public function pinVerify($request)
    {
        $DataArray = [];
        $client = new Client();
        $route = 'http://b*******e.com/****/P***y.php';
        $params = [
            'form_params' => [
                'Username' => $this->subUsername,
                'Password' => $this->subPassword,
                'userID' => $request->user_id,
                'pincode' => $request->pin_code,
            ]
        ];

        $result = $client->request('POST', $route, $params);
        $body = $result->getBody();

        $bodyContent = $body->getContents();
        if ($bodyContent === 1) {
            $DataArray['message'] = 'Failed because of system error';
            $DataArray['status'] = 'failed';
        } else {
            $DataArray['message'] = 'Sorry provided pincode is wrong.';
            $DataArray['status'] = 'failed';
        }

        return $DataArray;
    }
}

In the controller, I am using it in one method like below.

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\SubscriptionService as Subscription;

class XyzController extends Controller
{
    public function verifyPinCode(Subscription $Subscription, Request $request){
        $serviceResponse = $Subscription->pinVerify($request);
        return response()->json($serviceResponse, 200); 
    }
}

In the result I am getting the error Class does not exist. I am not sure where I am making a mistake. Can someone kindly guide me on how to fix the issue?

Error

ReflectionException
Class App\Services\SubscriptionService does not exist
Previous exceptions
syntax error, unexpected ''pincode'' (T_CONSTANT_ENCAPSED_STRING)

Composer.json

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },

enter image description here

3 Answers

Everything seems okay. Try giving the command

composer dump-autoload

There is an extra space or something after given line. I have checked your code and just hit a backspace after this line 'userID' => $request->user_id,​and your syntax error, unexpected ''pincode'' (T_CONSTANT_ENCAPSED_STRING) error is gone

The syntax error, unexpected ''pincode'' (T_CONSTANT_ENCAPSED_STRING) means that you have a parsing issue (extra character or missing character or something like that) in the file of SubscriptionService class. Therefore, the file is not parsed and the class is not available. The exception error trace is clear about that. Find the issue and solve it. You problem will be solved.

Related