【问题标题】:Laravel/Socialite: Class Laravel\Socialite\Contracts\Factory does not existLaravel/Socialite:类 Laravel\Socialite\Contracts\Factory 不存在
【发布时间】:2016-07-11 00:07:53
【问题描述】:

我正在尝试实现社交名流,但我收到了与 Factory 类有关的错误。我的应用找不到它。

这是我控制器中的代码:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Laravel\Socialite\Contracts\Factory as Socialite;

class PortalController extends Controller
{

    public function __construct(Socialite $socialite){
           $this->socialite = $socialite;
       }


    public function getSocialAuth($provider=null)
    {
       if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist

       return $this->socialite->with($provider)->redirect();
    }


    public function getSocialAuthCallback($provider=null)
    {
       if($user = $this->socialite->with($provider)->user()){
          dd($user);
       }else{
          return 'something went wrong';
       }
    }

我补充说:

Laravel\Socialite\SocialiteServiceProvider::class, 提供者和

'Socialite' =&gt; Laravel\Socialite\Facades\Socialite::class 转别名

我的路线看起来像

Route::get('/portal/{provider?}',[
        'uses' => 'PortalController@getSocialAuth',
        'as'   => 'portal.getSocialAuth'
    ]);


    Route::get('/portal/callback/{provider?}',[
        'uses' => 'PortalController@getSocialAuthCallback',
        'as'   => 'portal.getSocialAuthCallback'
    ]);

我收到的错误是:

Container.php 第 798 行中的反射异常: Laravel\Socialite\Contracts\Factory 类不存在

【问题讨论】:

    标签: php laravel laravel-socialite


    【解决方案1】:

    我在Laravel 5.5 在创建自定义 oAuth 提供程序时也遇到了这个问题。经过长时间的研究,我通过创建自定义MySocialServiceProvider 类来实现需要扩展Laravel\Socialite\SocialiteServiceProvider。请通过以下所有代码并使用适当的配置进行设置,它肯定会起作用。

    我的目录结构如下图

    MySocialServiceProvider.php

    <?php
    
    namespace App\Providers;
    
    use Laravel\Socialite\SocialiteServiceProvider;
    
    class MySocialServiceProvider extends SocialiteServiceProvider
    {
        public function register()
        {
            $this->app->bind('Laravel\Socialite\Contracts\Factory', function ($app) {
                return new MySocialManager($app);
            });
        }
    }
    

    我们必须创建一个 Manger 类,它将包含以下内容

    MySocialManager.php

    <?php
    
    namespace App\Providers;
    
    use App\Auth\SocialiteFooDriver;
    use Laravel\Socialite\SocialiteManager;
    
    class MySocialManager extends SocialiteManager
    {
        protected function createFooDriver()
        {
            $config = $this->app['config']['services.foo'];
    
            return $this->buildProvider(
                SocialiteFooDriver::class, $config
            );
        }
    }
    

    我们应该创建一个供MySocialManger使用的自定义驱动程序

    SocialiteFooDriver.php

    <?php
    
    namespace App\Auth;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Arr;
    use Laravel\Socialite\Two\AbstractProvider;
    use Laravel\Socialite\Two\ProviderInterface;
    use Laravel\Socialite\Two\User;
    
    class SocialiteFooDriver extends AbstractProvider implements ProviderInterface
    {
        /**
         * Foo API endpoint.
         *
         * @var string
         */
    //    protected $apiUrl = 'https://auth.foobar.com';
        protected $apiUrl = '';
    
        public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl)
        {
            parent::__construct($request, $clientId, $clientSecret, $redirectUrl);
            $this->apiUrl = config('services.foo.url');
        }
    
        /**
         * The scopes being requested.
         *
         * @var array
         */
        protected $scopes = ['openid email profile user_role user_full_name'];
    
        /**
         * {@inheritdoc}
         */
        protected function getAuthUrl($state)
        {
            return $this->buildAuthUrlFromBase($this->apiUrl.'/oauth2/authorize', $state);
        }
    
        /**
         * {@inheritdoc}
         */
        protected function getTokenUrl()
        {
            return $this->apiUrl.'/oauth2/token';
        }
    
        /**
         * {@inheritdoc}
         */
        protected function getUserByToken($token)
        {
            $userUrl = $this->apiUrl.'/oauth2/UserInfo?access_token='.$token;
    
            $response = $this->getHttpClient()->get(
                $userUrl, $this->getRequestOptions()
            );
    
            $user = json_decode($response->getBody(), true);
    
            if (in_array('user:email', $this->scopes)) {
                $user['email'] = $this->getEmailByToken($token);
            }
    
            return $user;
        }
    
        /**
         * Get the POST fields for the token request.
         *
         * @param string $code
         *
         * @return array
         */
        protected function getTokenFields($code)
        {
            return array_add(
                parent::getTokenFields($code), 'grant_type', 'authorization_code'
            );
        }
    
        /**
         * {@inheritdoc}
         */
        protected function mapUserToObject(array $user)
        {
            return (new User())->setRaw($user)->map([
                'id' => $user['sub'],
                'nickname' => $user['preferred_username'],
                'name' => Arr::get($user, 'name'),
                'email' => Arr::get($user, 'email'),
                'avatar' => $user['avatar'],               
    
            ]);
        }
    
        /**
         * Get the default options for an HTTP request.
         *
         * @return array
         */
        protected function getRequestOptions()
        {
            return [
                'headers' => [
                    //'Accept' => 'application/vnd.github.v3+json',
                ],
            ];
        }
    }
    

    最后我们应该在 config/services.php 中添加配置值

    'foo' => [
            'client_id' => 'XXXXXXXX',
            'client_secret' => 'YYYYYYYY',
            'redirect' => 'http://example.com/login/foo/callback/',
            'url' => 'https://auth.foobar.com',
        ],
    

    别忘了用我们的新提供者更新 config/app.php

    'providers' => [
    //...
    
     \App\Providers\MySocialServiceProvider::class
    
    ]
    

    【讨论】:

    • 太棒了,你拯救了我的一天
    【解决方案2】:

    “composer update”为我解决了这个问题,它适用于:“use Laravel\Socialite\Contracts\Factory as Socialite;”

    【讨论】:

      【解决方案3】:

      doc 开始,在config/app.php 文件中将Socialite libraryfacade 添加到各自的providersaliases 数组后,您只需要将Socialite 用作

      use Socialite;
      

      但你正在使用

      use Laravel\Socialite\Contracts\Factory as Socialite;
      

      所以,只需用

      删除上面的行
      use Socialite;
      

      根据评论更新

      composer update
      

      composer dump-autoload
      

      它应该工作。

      【讨论】:

      • 你做了composer dump-autoload 吗??
      • 然后试试use Laravel\Socialite\Facades\Socialite;
      • 我也试过了,但不起作用:“Class Laravel\Socialite\Facades\Socialite 不存在”
      • 我想你还没有安装这个包。您的供应商文件夹中有 Socialite 文件夹吗??
      • 它现在可以与“使用 Laravel\Socialite\Facades\Socialite;”一起使用在我更新作曲家之后。非常感谢您的帮助!
      【解决方案4】:

      Official installation guide 说你需要使用这个:

      use Socialite;
      

      代替:

      use Laravel\Socialite\Contracts\Factory as Socialite;
      

      如果不起作用,请尝试使用:

      use Laravel\Socialite\Facades\Socialite
      

      然后使用composer dumpauto

      【讨论】:

      • 尝试使用composer dumpauto。如果不行,试试use Laravel\Socialite\Facades\Socialite;
      • 你能告诉我使用use Socialite时导致错误的那一行吗?
      • 它现在与“使用 Laravel\Socialite\Facades\Socialite;”一起使用在我更新作曲家之后。非常感谢您的帮助!
      • 很高兴它有帮助。如果您想感谢我的时间,请选择我的答案作为最佳答案。 )
      猜你喜欢
      • 2018-01-02
      • 2016-06-22
      • 2016-08-08
      • 2016-04-20
      • 2018-10-10
      • 2020-07-07
      • 2018-03-22
      • 2019-10-07
      • 2023-02-08
      相关资源
      最近更新 更多