0

そこで、Laravel 4.1 で Sentry 2 を実装しようとしています。このリソースを使用しています

https://github.com/rydurham/L4withSentry

すべて正常に動作しますが、登録時にユーザーをグループに自動的に割り当てたいと考えています。

セントリー 2 ドキュメントから、

    // Find the group using the group id
    $adminGroup = Sentry::findGroupById(5);

    // Assign the group to the user
    $user->addGroup($adminGroup);

動作するはずです。そこで、このコード行を Authority\Repo\User\SentryUser.php に追加しました

コードは次のようになります

try {
    //Attempt to register the user. 
    $user = $this->sentry->register(array(
        'username' => e($data['username']),
        'email' => e($data['email']), 
        'password' => e($data['password'])
        ));


       // Find the group using the group id
       $adminGroup = Sentry::findGroupById(5);
      // Assign the group to the user
       $user->addGroup($adminGroup);

    //success!
            $result['success'] = true;
            $result['message'] = trans('users.created');
            $result['mailData']['activationCode'] = $user->GetActivationCode();
    $result['mailData']['userId'] = $user->getId();
    $result['mailData']['email'] = e($data['email']);
}

しかし、これはエラーをスローします

Non-static method Cartalyst\Sentry\Sentry::findGroupById() should not be called statically, assuming $this from incompatible context 

誰かが光を当てて、私が間違っていることを教えてもらえますか? 前もって感謝します

4

1 に答える 1

2

どういうわけか、Sentry を使用している場合、Facade を使用しているのではなく、クラス自体を使用しています。Facade を介してクラスを呼び出す場合、実際には statics を使用しているのではなく、実際に使用しているように見えます。

これありませんか:

use Cartalyst\Sentry\Sentry;

あなたのコードで?

わかりましたが、この行が機能している場合:

$user = $this->sentry->register(array(
    'username' => e($data['username']),
    'email' => e($data['email']), 
    'password' => e($data['password'])
    ));

したがって、すでにインスタンス化されており、確実に実行できます。

$adminGroup = $this->sentry->findGroupById(5);
于 2013-12-31T22:23:05.587 に答える