[英]Lumen + Socialite - Laravel\Socialite\Contracts\Factory is not instantiable
[英]Laravel/Socialite: Class Laravel\Socialite\Contracts\Factory does not exist
我正在嘗試實施社交名媛,但遇到與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' => 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行中的ReflectionException:類Laravel \\ Socialite \\ Contracts \\ Factory不存在
在創建自定義oAuth提供程序時,我也在Laravel 5.5
遇到了此問題。 經過長時間的研究,我通過創建自定義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
]
從文檔中 ,將Socialite library
和facade
添加到config/app.php
文件中的相應providers
和aliases
數組之后,您只需要使用社交網站作為
use Socialite;
但是你在用
use Laravel\Socialite\Contracts\Factory as Socialite;
因此,只需刪除上面的行
use Socialite;
已從評論更新
composer update
和
composer dump-autoload
它應該工作。
官方安裝指南說您需要使用此:
use Socialite;
代替:
use Laravel\Socialite\Contracts\Factory as Socialite;
如果不起作用,請嘗試使用:
use Laravel\Socialite\Facades\Socialite
然后使用composer dumpauto
。
“ composer update”為我解決了此問題,它可以與“將Laravel \\ Socialite \\ Contracts \\ Factory用作Socialite”一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.