CakePHP:保存后更新会话变量

发布于 2024-07-23 18:24:35 字数 569 浏览 6 评论 0原文

我有一个 User 对象,在成功进行身份验证后,该对象将被隐藏到会话中(无安全信息),以便于调用并确定我们是否有经过身份验证的用户或匿名会话。 用户可以通过多种途径更改他或她的部分或全部信息,我希望使该会话值保持最新。 显而易见的答案是更新 afterSave() 回调中的值,但这当然违反了 MVC。

是否有另一种方法可以捕获一个地方的每个更改,这样我就不必在各处删除会话写入? 我想不出什么,也找不到任何其他想法。 我是唯一一个尝试做这样的事情的人吗?

谢谢。

最终解决方案:坦白说,我将 neilcrookes 的回复标记为答案,因为似乎没有更好的方法。 然而,由于这种方式违反了我的强迫症感觉,所以我采取了略有不同的道路。 我决定让我的 User::authenticate() 方法将经过身份验证的用户对象返回给调用者,以便它可以对其执行任何操作。 调用者“想要”做的事情之一就是在会话中删除该值。 这是冗余,但非常非常有限。 在我看来,这比从模型访问会话感觉更好(尽管如果你这样做,那肯定是该死的,如果你不场景,那肯定是该死的)。

I have a User object that, upon successful authentication, is tucked into the session (sans security info) for easy recall and for determining whether we have an authenticated user or anonymous session. There are several paths by which the user can alter some or all of his or her information and I'd like to keep that session value up to date. The obvious answer is to update the value in the afterSave() callback, but that, of course, violates MVC.

Is there another way of capturing every change in one place so that I don't have to drop session writes all over the place? I can't think of anything, nor have I been able to find any other ideas. Am I the only person trying to do something like this?

Thanks.

Final Solution: I marked neilcrookes' response as the answer, frankly, because there doesn't seem to be the better way. Since this way violates my OCD senses, though, I took a slightly different path. I decided to have my User::authenticate() method return the authenticated user object to the caller so it can do whatever it wants with it. One of the things that the callers "want" to do is to drop that value in the session. It's redundancy, but it's very, very limited. In my mind, that felt better than accessing the session from the model (though it's certainly a damned if you do, damned if you don't scenario).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甲如呢乙后呢 2024-07-30 18:24:35
//in users controller 
if ($this->User->save()) {
    $this->Auth->login($this->User->read());
    $this->Session->setFlash[.. etc]

郑重声明,我不同意尼尔克鲁克斯的回答,但我不会喂食巨魔。

//in users controller 
if ($this->User->save()) {
    $this->Auth->login($this->User->read());
    $this->Session->setFlash[.. etc]

And for the record, I do not agree with the answer of neilcrooks, but I will refrain from feeding the troll.

自控 2024-07-30 18:24:35

有些人可能不同意,但我会搞砸 MVC,在 Model::afterSave() 中执行它并使用 $_SESSION - 在写入会话之前测试会话,以防它没有启动,例如您正在 shell 中保存模型或某物。

MVC 是一种通用模式 - 一种指导方针,您可以用头来反对它,试图弄清楚如何实现不太适合的东西,或者只是用另一种方式去做,然后转向更重要的事情。

点燃火焰。

Some might disagree but I'd screw MVC, do it in Model::afterSave() and use $_SESSION - test for the session before writing to it, in case it's not started for example you are saving against the model in a shell or something.

MVC is a general pattern - a guideline, you can bang your head against it trying to figure out how to achieve something that doesn't quite fit, or just do it another way and move onto to something more important.

Bring on the flames.

三寸金莲 2024-07-30 18:24:35

保存后

使用像这样

$this->Session->write('Auth.User.mmid', $kinde['Kindle']['id']);

after save

Use Like this

$this->Session->write('Auth.User.mmid', $kinde['Kindle']['id']);
北城半夏 2024-07-30 18:24:35

您应该能够仅使用 AppController 创建必要的回调来保持会话数据最新。 因此,例如,您可以让您的 User 模型 afterSave() 将名为 changed 的属性设置为 true。 然后在您的 AppController->afterFilter() 中检查该属性并根据需要更新会话数据。

或者,您可以编写一个组件来更新您的用户信息和会话数据。 然后任何需要更改用户信息的控制器只需要包含该组件。

无需编写冗余代码或破坏 MVC。

You should be able to just use AppController to create the necessary callback(s) that keep your session data up to date. So, for instance, you could have your User model afterSave() set a property called changed to true. Then in your AppController->afterFilter() you check that property and update the session data as necessary.

Alternatively, you could write a component through which to update your user info and also your session data. Then any controller that needs to change user info just needs to include that component.

There's no need to write redundant code or break MVC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文