【发布时间】:2017-01-06 23:28:17
【问题描述】:
我正在尝试在 laravel 5.3 中实现谷歌登录。我使用composer安装了hybridauth:composer require hybridauth/hybridauth
但是,首先我得到了错误:
“找不到类 Hybrid_Auth”
所以我为 Hybridauth 中的每个类添加了一个命名空间 namespace Hybridauth;(项目中的路径:vendor/hybridauth/hybridauth/hybridauth/Hybrid),并使用 use Hybridauth\Hybrid_Auth 将其包含在我的控制器中;但现在我得到了错误:
Auth.php 第 16 行中的 FatalErrorException:“无法声明类 Hybridauth\Hybrid_Auth,因为名称已经在使用中”
尽管我已经搜索了整个项目并且 Hybrid_Auth 类只声明了一次。
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hybridauth\Hybrid_Auth;
class AuthController extends Controller
{
public function googleLogin($auth=null)
{
if($auth == 'auth')
{
try
{
Hybrid_Endpoint::process();
}
catch(Exception $e)
{
return Redirect::to('googleAuth');
}
return;
}
$config = array(
"base_url" => "localhost:8000/gauth",
"providers" => array (
"Google" => array (
"enabled" => true,
"keys" => array ( "id" => "googleIdGoeshEre", "secret" => "googleSecretKeyGoeshEre" ),
"scope" => "https://www.googleapis.com/auth/userinfo.profile ". // optional
"https://www.googleapis.com/auth/userinfo.email" , // optional
"access_type" => "offline", // optional
"approval_prompt" => "force", // optional
"hd" => "domain.com" // optional
)));
$oauth = new Hybrid_Auth($config);
$provider = $oauth->authenticate("Google");
$profile = $provider->getUserProfile();
return var_dump($profile).'<br><a href="logout">Logout</a>';
}
}
?>
【问题讨论】: