php - 尝试将 Guzzle Curl 客户端绑定(bind)到 Laravel 的服务容器——然后在尝试 __construct() 时键入提示客户端失败

标签 php laravel laravel-5 ioc-container type-hinting

所以我想我会尝试在 Laravel 中实际使用这个奇特的 IoC 容器。我从 Guzzle 开始,但我无法让它工作。也许我的理解有差距。我真的很感谢这里的任何帮助。

所以我有一个用于连接到 RESTful Api 的类。这是其中的一个示例:

    use GuzzleHttp\Exception\BadResponseException;
    use GuzzleHttp\Client;
    use GuzzleHttp\Subscriber\Oauth\Oauth1;

class EtApi {
    //you can pass in the model if you wanna
    //protected $model;

    //client Id
    protected $clientId;

    //client secret
    protected $clientSecret;

    //base_uri
    protected $getTokenUri;

    protected $client;

    //build
    function __construct(Client $client)
    {
        $this->client = $client;
        $this->clientId = 's0m3R4nd0mStr1nG';
        $this->clientSecret = 's0m3R4nd0mStr1nG';
        $this->getTokenUri = 'https://rest.api/requestToken';
        $this->accessToken = $this->getToken($this->clientId, $this->clientSecret, $this->getTokenUri);
    }

我通过在 $client = new Client(); 这样的方法中手动更新它,成功地安装和使用了 Guzzle。但这不是很干,也不是正确的做事方式。所以我在 app\Providers\GuzzleProvider.php 创建了一个 ServiceProvider。我确定这是在 $providers = ['App\Providers\GuzzleProvider'] 下的 app/config/app.php 中注册的。这是提供商代码:

    <?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Oauth\Oauth1;

class GuzzleProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->bind('Client', function () {
            return new Client;
        });
    }

}

因此,当我尝试访问我的 EtApi 方法时,在实例化 (__construct) 期间加载失败并出现以下错误。

ErrorException in EtApi.php line 23:
Argument 1 passed to App\EtApi::__construct() must be an instance of GuzzleHttp\Client, none given, called in /home/vagrant/webdocs/et_restful_test/app/Http/Controllers/EtConnectController.php on line 23 and defined

你们中的 Laravel 大师是否知道为什么我不能使用这段代码绑定(bind) Guzzle 而 Laravel 的魔法只会将 obj 注入(inject)构造函数? [文档 1说我应该可以做到这一点。我肯定错过了什么。谢谢!

最佳答案

根据你问题中的信息,有点难以肯定,但基于此

Argument 1 passed to App\EtApi::__construct() must be an instance of GuzzleHttp\Client, none given, called in /home/vagrant/webdocs/et_restful_test/app/Http/Controllers/EtConnectController.php on line 23 and defined

听起来您是在 EtConnectController.php 的第 23 行直接实例化您的 App\Eti 类,代码看起来像这样

$api = new App\EtApi;

如果是这样,那么您就缺少了 Laravel 依赖注入(inject)的一个关键部分。 Laravel 无法改变标准 PHP 的行为——也就是说,如果您使用 PHP 的内置 new 关键字创建一个新类,那么 Laravel 永远不会改变以在 __construct< 中注入(inject)任何依赖项.

如果您想利用依赖注入(inject),您还需要通过 Laravel 的应用程序容器实例化您的对象。有很多不同的方法可以做到这一点——这里有两种

//$api = new App\EtApi;
\App::make('App\EtApi');     //probably "the right" way
$api   = app()['App\EtApi']

如果你这样做,Laravel 将读取 __construct 中的类型提示并尝试为你的对象注入(inject)依赖项。

关于php - 尝试将 Guzzle Curl 客户端绑定(bind)到 Laravel 的服务容器——然后在尝试 __construct() 时键入提示客户端失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654380/

相关文章:

javascript - Stripe Payment Element 加载时间慢

php - Google Charts & MySQL Timestamp 处理问题

php - 如何制作列的全文索引?

laravel - https 请求 "invalid request (unsupported ssl request)"

laravel - 如何在 Laravel 5 中建立用户和角色关系

基于PHP OOP的登录系统

php foreach作为变量

php - 捕获 FatalErrorException PHP

javascript - jQuery AJAX 在 Laravel 5.2 中使用 jQuery 空闲超时中止

使用 Auth::User 进行 Laravel 查询