0

所以,我正在尝试用 Laravel 4.1 实现 Sentry 2。我正在使用这个资源

https://github.com/rydurham/L4withSentry

一切正常,但现在我想在用户注册时自动将用户分配给一个组。

来自 Sentry 2 Docs,

    // 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 调用一个类时,你并没有真正使用静态,它只是看起来像你。

你有这个吗:

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 回答