使用 Slim Framework 的 Cartalyst Sentinel 原生激活问题

Activation issue with Cartalyst Sentinel native with Slim Framework

我想在 Slim 框架中原生使用 Cartalyst-Sentinel(不是 Laravel)。 Sentinel 对象工作正常(我使用 Sentinel::register 方法没有问题)但是当我使用 Activation 对象时(使用 Activation::create() 方法的示例),收到以下错误:

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

这是我的代码:

    $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"
},

谢谢

因此,如果我们查看您收到的错误消息:

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

"non-object" 是您的 $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 情况下它应该做什么。

这是因为 Sentinel 提供的激活 class 由于某些奇怪的原因仅受 Laravel 直接支持而不是本地 Laravel/Database 库。

如果可能,请考虑改用 Sentry。它也是由 Cartalyst 制作的,它本质上是相同的库,但功能较少,但总体而言错误更少,并且比 Sentinel 更好地管理依赖关系。它还有更可靠的整体文档。

编辑:您可以通过替换来获取 Native 的激活存储库...

Activation::Sentinel::getActivationRepository()

只需使用 ,它就是这样与我一起工作的:

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

return 好则为 1,否则为空。