找不到方法

Method get not found

我创建了自己的 ServiceProvider 以在启动时发送参数形式 ENV class:

class InstagramServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function boot()
{
    $this->publishes([
        __DIR__.'/../config/config.php' => config_path('instagram.php'),
    ]);
}
public function register()
{
    $this->app->bind('instagram.client', function ($app) {
        return new InstagramClient([
            'apiKey' => $app['config']->get('instagram.clientId'),
            'apiSecret' => $app['config']->get('instagram.clientSecret'),
            'apiCallback' => $app['config']->get('instagram.redirectUri'),
            'scope' => $app['config']->get('instagram.scope'),
       ]);
    });
}
public function provides()
{
        return array('instagram.client');
}
}

我无法从配置中获取数据?我做错了什么?

Laravel 有一个专门为这个工作构建的函数,叫做 config()。您可以按如下方式使用它:

public function register()
{
    $this->app->bind('instagram.client', function ($app) {
        return new InstagramClient([
            'apiKey' => config('instagram.clientId'),
            'apiSecret' => config('instagram.clientSecret'),
            'apiCallback' => config('instagram.redirectUri'),
            'scope' => config('instagram.scope'),
       ]);
    });
}

或者,您可以使用括号表示法,如下所示:

$app['config']['instagram']['clientId']

有关详细信息,请参阅文档:https://laravel.com/docs/5.4/configuration#accessing-configuration-values