Yii2 Facebook登录问题

时间:2015-12-03 10:36:02

标签: php facebook yii2

我试图在我的网站上实现facebook登录。我遵循这个方法tutorial。一切都很好......但是当我运行网站时出现错误 Class' yiisoft \ authclient \ widgets \ AuthChoice'找不到
我附上了我的composer.json和view.php以及这个 composer.json

"require": {
    "php": ">=5.4.0",
    "yiisoft/yii2": ">=2.0.5",
    "yiisoft/yii2-bootstrap": "*",
    "yiisoft/authclient": "*",
    "yiisoft/yii2-swiftmailer": "*"
},  

我的观点

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use app\models\User;
use vendor\yiisoft\authclient\widgets\AuthChoice;
     <?= yiisoft\authclient\widgets\AuthChoice::widget([
     'baseAuthUrl' => ['site/auth']
]) ?>

并在我的网站

'authClientCollection' => [
      'class' => 'yiisoft\authclient\Collection',
      'clients' => [
        'facebook' => [
          'class' => 'yiisoft\authclient\clients\Facebook',
          'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
          'clientId' => '************',
          'clientSecret' => '*********',
        ],
      ],
    ],  

添加了这些代码..
任何人都可以告诉我为什么会这样......

2 个答案:

答案 0 :(得分:1)

尝试:

yii\authclient\widgets\AuthChoice::widget

信息:http://www.yiiframework.com/doc-2.0/yii-authclient-widgets-authchoice.html

答案 1 :(得分:0)

composer.json 在require部分添加此扩展名

 "yiisoft/yii2-authclient": "*"

首先:frontend / config / main.php 需要粘贴

'authClientCollection' => [
    'class' => 'yii\authclient\Collection',
    'clients' => [
    'facebook' => [
    'class' => 'yii\authclient\clients\Facebook',
    'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
    'clientId' => '13232324242322424',
    'clientSecret' => '32jjkschsuc6dsgdjhsd7dtusdjsds7',
    'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
         ],
    ],

    ],

第二:前端/控制器/ sitecontroller 需要粘贴=======&gt;&gt;&gt;

public function actions()
{
    return [
    'auth' => [
    'class' => 'yii\authclient\AuthAction',
    'successCallback' => [$this, 'oAuthSuccess'],
         ],
    ];

}

public function oAuthSuccess($client) 
{
    $userAttributes = $client->getUserAttributes();
    $user_email= $userAttributes['email'];
    $user_first_name = $userAttributes['first_name'];
    $user_last_name  = $userAttributes['last_name'];
    $model = new SignupForm();
    $user = $model->facebook_data($user_email,$user_last_name,$user_first_name);
    return $this->render('signup', [
        'model' => $model,
    ]);

    exit;
    // do some thing with user data. for example with $userAttributes['email']
}

第三步:: 保存first_name,用户表中的last_name需要创建其他属性

public function facebook_data($user_email,$user_last_name,$user_first_name)
{
    $user = new User();
    $user->email= $user_email;
    $user->first_name =  $user_first_name;
    $user->last_name = $user_last_name;
    return $user->save() ? $user : null;
}    

第四步:: 来自facebook开发者网站需要获取应用ID并在第一步提供密钥和粘贴,其中提到了&#39; clientId&#39; =&GT; &#39; xxxxxxxxxxxxxxxxx&#39 ;,         &#39; clientSecret&#39; =&GT; &#39; xxxxxxxxxxxxxxxxxxxxxxxxxxx&#39 ;, 对于localhost需要给url。 例如:我的网址是 http://localhost/noteshub/advanced/frontend/web/index.php?r=site%2Flogin 我只是粘贴在facebook网站的网址上,同时添加网站以获得2个密钥。

第五步: 在视图文件中需要粘贴这个为你点击的Facebook图标...

 <?= yii\authclient\widgets\AuthChoice::widget([
                   'baseAuthUrl' => ['site/auth']
                  ]) ?>

在视图文件中自动查看facebook图标