【问题标题】:Using HybridAuth in Laravel 5 Error Cannot declare class Hybrid_Auth在 Laravel 5 中使用 HybridAuth 错误无法声明类 Hybrid_Auth
【发布时间】: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>';
    }
    }
?>

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    所以我添加了一个命名空间命名空间Hybridauth;到每个班级 Hybridauth(项目中的路径: 供应商/hybridauth/hybridauth/hybridauth/Hybrid)

    编辑vendor文件夹中的文件不是一个好主意,因为当你这样做composer update时,这些文件会得到更新,那么你必须再次编辑。

    我建议您删除您在供应商文件夹中添加的命名空间。并在Controller中添加use Hybrid_Auth;

    确保您包含混合身份验证。 require_once( "/path/to/hybridauth/Hybrid/Auth.php");

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2015-09-15
      • 1970-01-01
      • 2019-01-15
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多