Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a class from vendor folder in laravel project

I am trying to include guzzle http client from a vendor folder and using composer. Here is what I have tried so far.

Location of guzzle http client file vendor/guzzle/guzzle/src/Guzzle/Http/Client.php

In composer.json file I included

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files":["vendor/guzzle/guzzle/src/Guzzle/Http/Client.php"],
    "psr-4": {
        "App\\": "app/"
    }
},

The I ran the command composer dumpautoload.

In my controller I am trying to call an api end point like this

use GuzzleHttp\Client;
$client = new Client(); // this line gives error 
$res = $client->get('https://api.fixer.io/latest?symbols=CZK,EURO');

The error is Class 'GuzzleHttp\Client' not found

What I am missing here, please help me. Thanks.

For a better file structure here is a screenshot of of the file location enter image description here

like image 843
Hkm Sadek Avatar asked Dec 28 '17 19:12

Hkm Sadek


People also ask

What is use of vendor folder in laravel?

Laravel's Vendor directory The vendor directory contains the composer dependencies, for example, to install Laravel setup, the composer is required. The vendor folder contains all the composer dependencies.

Where is vendor folder in laravel?

The vendor is a subfolder in the Laravel root directory. It includes the Composer dependencies in the file autoload. php. Composer is a PHP based tool for dependency management.


1 Answers

Short Version: You're trying to instantiate a class that doesn't exist. Instantiate the right class and you'll be all set.

Long Version: You shouldn't need to do anything fancy with your composer.json to get Guzzle working. Guzzle adheres to a the PSR standard for autoloading, which means so long as Guzzle's pulled in via composer, you can instantiate Guzzle classes without worrying about autoloading.

Based on the file path you mentioned, it sounds like you're using Guzzle 3. Looking specifically at the class you're trying to include

namespace Guzzle\Http;
/*...*/
class Client extends AbstractHasDispatcher implements ClientInterface
{
        /*...*/
}

The guzzle client class in Guzzle 3 is not GuzzleHttp\Client. Its name is Guzzle\Http\Client. So try either

$client = new \Guzzle\Http\Client;

or

use Guzzle\Http\Client;
$client = new Client;

and you should be all set.

like image 107
Alan Storm Avatar answered Nov 14 '22 22:11

Alan Storm