在Slim框架下催化剂哨兵的激活问题


Activation issue with Cartalyst Sentinel native with Slim Framework

我想在Slim框架(而不是Laravel)中本地使用catalyst - sentinel。哨兵对象工作正常(我使用Sentinel::register方法没有问题),但当我使用激活对象(Activation::create()方法的示例)时,收到以下错误:

在…'vendor'illuminate'support'Facades'Facade.php中调用成员函数create()

这是我的代码:

    $data = Sentinel::register($credentials);
    $user = Sentinel::findById($data['id']);
    $activation = Activation::create($user);

是我的composer.json的一部分:

"require": {
    "slim/slim": "^2.6",
    "entomb/slim-json-api": "dev-master",
    "symfony/http-foundation": "^2.7",
    "swiftmailer/swiftmailer": "^5.4",
    "respect/validation": "^0.9.3",
    "cartalyst/sentinel": "^2.0",
    "illuminate/database": "^5.1",
    "illuminate/events": "^5.1"
},

谢谢

这是因为由于一些奇怪的原因,Sentinel提供的激活类只被Laravel直接支持,而不是原生的Laravel/Database库。

如果可能的话,考虑使用哨兵。它也是由Cartalyst制作的,本质上是相同的库,功能更少,但总体上似乎bug更少,并且比Sentinel更好地管理依赖关系。它也有更可靠的文档。

编辑:您可以通过替换…来获得本机的激活存储库

Activation:: with Sentinel::getActivationRepository()

就这样用,它就像这样和我一起工作:

$data = Sentinel::register($credentials);
$user = Sentinel::findById($data['id']);
$activation = Sentinel::activate($user);

正确返回1,不正确返回空

如果我们看一下你得到的错误信息:

Call to a member function create() on a non-object in ...'vendor'illuminate'support'Facades'Facade.php on line 210

那个"非对象"是你的$user变量。在我看来,Sentinel::findById($data['id']);应该通过查找提供的id来返回一个代表用户的对象。由于某种原因,它没有找到该用户,因此它可能返回nullfalse。如果您的应用程序可以接受这种行为,那么您可以这样做:

$data = Sentinel::register($credentials);
$user = Sentinel::findById($data['id']);
if ($user){
    // The user was successfully found
    $activation = Activation::create($user);
} else {
    // Generate an error/exception/message here indicating that the user could not be found, or take them to the 404 page, etc.
    ...
}

我不太了解你的应用程序,不能说它在else的情况下应该做什么。