带有Sentry 2的Laravel 4将用户添加到注册组中

时间:2013-12-31 22:08:36

标签: php laravel laravel-4 cartalyst-sentry

所以,我试图用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);

应该有效。所以我添加了这行代码 权威\回购\用户\ 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 

任何人都可以光明并告诉我我做错了什么吗? 在此先感谢

1 个答案:

答案 0 :(得分: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);