我如何在Laravel项目中使用供应商文件夹中的类 [英] How can I use a class from vendor folder in laravel project

查看:97
本文介绍了我如何在Laravel项目中使用供应商文件夹中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从供应商文件夹中加入guzzle http客户端,并使用作曲家.到目前为止,这是我尝试过的.

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

guzzle http客户端文件的位置 vendor/guzzle/guzzle/src/Guzzle/Http/Client.php

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

在composer.json文件中,我包含了

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/"
    }
},

我运行了 composer dumpautoload 命令.

在我的控制器中,我试图像这样调用api端点

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');

错误是找不到类'GuzzleHttp \ Client'

我在这里所缺少的,请帮助我.谢谢.

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

为获得更好的文件结构,以下是文件位置的屏幕截图

推荐答案

简短版本:您正在尝试实例化一个不存在的类.实例化正确的类,您便会准备就绪.

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

长版:您无需对composer.json做任何花哨的操作即可使Guzzle正常工作.Guzzle遵循自动加载的PSR标准,这意味着只要通过作曲家将Guzzle插入,您就可以实例化Guzzle类,而不必担心自动加载.

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.

根据您提到的文件路径,听起来像您正在使用Guzzle 3,/a>.专门查看您要包含的类

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
{
        /*...*/
}

Guzzle 3中的guzzle客户端类不是 GuzzleHttp \ Client .它的名称是 Guzzle \ Http \ Client .所以尝试

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;

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

,您应该已经准备就绪.

and you should be all set.

这篇关于我如何在Laravel项目中使用供应商文件夹中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆