【问题标题】:Activation issue with Cartalyst Sentinel native with Slim Framework带有 Slim 框架的 Cartalyst Sentinel 本机激活问题
【发布时间】:2015-09-11 18:00:57
【问题描述】:

我想将 Cartalyst-Sentinel 与 Slim 框架(不是 Laravel)一起使用。 Sentinel 对象正常工作(我使用 Sentinel::register 方法没有问题)但是当我使用 Activation 对象时(以Activation::create()method 为例),收到以下错误:

在第 210 行的 ...\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"
},

谢谢

【问题讨论】:

  • 我确认 $user 是一个有效的对象。

标签: php slim cartalyst-sentinel


【解决方案1】:

这是因为 Sentinel 提供的 Activation 类仅由 Laravel 直接支持,而不是 Native Laravel/Database 库,出于某种奇怪的原因。

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

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

Activation::Sentinel::getActivationRepository()

【讨论】:

    【解决方案2】:

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

    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 案例中应该做什么。

    【讨论】:

    • 谢谢,但是 $user 对象是有效的。我已经检查过了。我想 Activation 实例没有被 Sentinel 初始化
    【解决方案3】:

    只需使用 ,它就这样和我一起工作:

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

    如果好则返回 1,否则为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多