【问题标题】:Lumen + Socialite - Laravel\Socialite\Contracts\Factory is not instantiableLumen + Socialite - Laravel\Socialite\Contracts\Factory 不可实例化
【发布时间】:2018-01-02 09:24:16
【问题描述】:

我想知道是否有人为 Lumen 实现了自定义社交名流提供程序。我正在尝试创建一个自定义提供程序,但没有这样做。该错误似乎与 Google 搜索中发现的类似问题相比是多余的,但它们都不适合我。我已经用尽了所有的选择。

以下是我用于提供程序的代码

bootstrap/app.php

$app->withFacades();

class_alias('Laravel\Socialite\Facades\Socialite', 'Socialite');


// socialite
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);

app/Providers/AppServiceProviders.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Briovo\Providers\BriovoProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->registerBriovoSocialite();       
    }

    private function registerBriovoSocialite()
    {
        $briovo = $this->app->make('Laravel\Socialite\Contracts\Factory');
        /*$briovo->extend(
            'briovo',
            function ($app) use ($briovo) {
                $config = $app['config']['services.briovo'];
                return $briovo->buildProvider(BriovoProvider::class, $config);
            }
        );*/
    }

}

app/Providers/BriovoProvider.php

<?php

namespace Briovo\Providers;

use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

class BriovoProvider extends AbstractProvider implements ProviderInterface {

    protected $base_url = 'http://central.dev'

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase( $base_url . '/authorize');
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return $base_url . '/oauth/token';
    }

    /**
     * {@inheritdoc}
     */
    public function getAccessToken($code)
    {
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
                'form_params' => [
                'grant_type' => 'authorization_code',
                'client_id' => 'client-id',
                'client_secret' => 'client-secret',
                'redirect_uri' => 'http://example.com/callback',
                'code' => $code,
            ],
        ]);

        return $this->parseAccessToken($response->getBody());
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenFields($code)
    {
        return array_add(
            parent::getTokenFields($code), 'grant_type', 'authorization_code'
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        $response = $this->getHttpClient()->get( $this->base_url . '/api/me', [
            'headers' => [
                'Authorization' => 'Bearer ' . $token,
            ],
        ]);

        return json_decode($response->getBody(), true);
    }


}

app/Http/Controller/BriovoAuthController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;

class BriovoAuthController extends Controller
{

    protected $provider_name = 'briovo';
    public function redirectToProvider()
    {
        //return Socialite::driver($this->provider_name)->redirect();
    }

    public function handleProviderCallback()
    {
        // $user = Socialite::driver($this->provider_name)->stateless()->user();
    }

}

问题

我遇到错误

BindingResolutionException
Target [Laravel\Socialite\Contracts\Factory] is not instantiable.

问题出在AppServiceProvider.php 文件中。

$briovo = $this-&gt;app-&gt;make('Laravel\Socialite\Contracts\Factory'); 行失败。我尝试从boot() 方法调用此代码,但没有效果。

我了解代码的其他部分现在可能不正确。在我弄清楚这个问题后,我可以让他们工作。据我了解,我已经很好地注册了外观和别名。

编辑1:不同的方法相同的结果

我尝试使用socialite provider generator,但我遇到了同样的错误!

我已按照流明的确切说明进行操作,但没有用!

【问题讨论】:

    标签: php laravel lumen laravel-socialite


    【解决方案1】:

    我在 laravel 5.5 中实现自定义社交派生时发现了这个问题。如问题中所述,我已经在 laravel 5.4 中实现了自定义社交驱动程序,并且工作正常。

    查看以下 StackOverflow 问题的答案可能会对您有所帮助。下面一个解决了我的问题。

    Laravel/Socialite: Class Laravel\Socialite\Contracts\Factory does not exist

    【讨论】:

      【解决方案2】:

      里面 app/providers/AppServiceProvider.php

      粘贴以下内容

      public function register()
      {
          $this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() {
              return new \Laravel\Lumen\Http\ResponseFactory();
          });
          $this->app->bind(\Illuminate\Contracts\Routing\UrlGenerator::class, function ($app) {
              return new \Laravel\Lumen\Routing\UrlGenerator($app);
        });
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-22
        • 2016-08-08
        • 2016-07-11
        • 2020-06-08
        • 2016-04-20
        • 1970-01-01
        • 2016-06-02
        • 2018-04-15
        • 2018-01-30
        相关资源
        最近更新 更多