Ошибка в социальном входе в Yii2: установка неизвестного свойства: yii \ web \ Application :: authClientCollection - PullRequest
0 голосов
/ 07 июня 2018

Когда я пытаюсь получить доступ к приложению, появляется ошибка после использования yii2-authclient.Я следую Аутентификация Facebook с использованием Yii2 authclient

Все настроено, но возникает эта ошибка:

Установка неизвестного свойства: yii \ web \ Application :: authClientCollection

this is error detail which is i faced during execution my yii app

Мой frontend/main.php:

'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//            'rules' => [
//            ],
//        ],
    ],
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
                'clientId' => '2061735130522390',
                'clientSecret' => '2f302dc7358730820c091ca4444afbae',
                'attributeNames' => ['name', 'email', 'first_name', 'last_name'],
            ],
        ],
    ],

Контроллер моего сайта:

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public $successUrl = "success";
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'successCallback'],
            ],
        ];
    }

    public function successCallback($client) {
        // get user data from client
        $userAttributes = $client->getUserAttributes();

        $user = User::find()->where(['email'=>$userAttributes['email']])->one();
        if (!empty($user))
        {
            Yii::$app->user->login($user);
        }
        else{
            $session = Yii::$app->session;
            $session['attribute'] = $userAttributes;
            $this->successUrl = Url::to(['signup']);
        }
        die(print_r($userAttributes));
        // do some thing with user data. for example with $userAttributes['email']
    }
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Logs in a user.
     *
     * @return mixed
     */
.
.
.

}

login.php - это:

<div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

                <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

                <?= $form->field($model, 'password')->passwordInput() ?>

                <?= $form->field($model, 'rememberMe')->checkbox() ?>

                <div style="color:#999;margin:1em 0">
                    If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
                </div>

                <div class="form-group">
                    <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
                </div>
            <p>OR</p>
            <?= yii\authclient\widgets\AuthChoice::widget([
                'baseAuthUrl' => ['site/auth']
            ]) ?>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

1 Ответ

0 голосов
/ 07 июня 2018

Ваш конфиг неверен, authClientCollection конфиг должен быть внутри массива components - в вашем случае он вне его.Вы должны переместить authClientCollection элемент на одну строку вверх и изменить это:

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

На это:

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