Using HybridAuth in Laravel 5 Error Cannot declare class Hybrid

I am trying to implement google login in laravel 5.3. I installed hybridauth using composer: composer require hybridauth/hybridauth

However, first i got the error:

"Class Hybrid_Auth not found"

So I added a namespace namespace Hybridauth; to each class in Hybridauth (path in project: vendor/hybridauth/hybridauth/hybridauth/Hybrid), and included it in my controller using use HybridauthHybrid_Auth; But now i get the error:

FatalErrorException in Auth.php line 16: "Cannot declare class HybridauthHybrid_Auth, because the name is already in use"

Even though I've searched the entire project and the class Hybrid_Auth is only declared once.

Here is my controller:

<?php
    namespace AppHttpControllers;
    use IlluminateHttpRequest;
    use HybridauthHybrid_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>';
    }
    }
?>

So I added a namespace namespace Hybridauth; to each class in Hybridauth (path in project: vendor/hybridauth/hybridauth/hybridauth/Hybrid)

Editing file in vendor folder is not a good idea, because when you do composer update those files will get update, then you have to edit again.

I suggest you remove the namespace you added in the vendor folder. And add use Hybrid_Auth; in the Controller

Make sure you are including the hybrid auth. require_once( "/path/to/hybridauth/Hybrid/Auth.php");

链接地址: http://www.djcxy.com/p/88650.html

上一篇: Hybridauth + composer:如何添加自定义提供程序

下一篇: 在Laravel 5中使用HybridAuth错误无法声明类Hybrid