Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: Updating a session variable after save

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).

like image 651
Rob Wilkerson Avatar asked Jun 08 '09 16:06

Rob Wilkerson


People also ask

How do you refresh a session variable?

Update Session Variable in PHP To update any value stored in the session variable, start the session by calling session_start() function and then simply overwrite the vakue to update session variable.

Can users edit session variables?

Modifying Published Session Variables Session variables on the client are read-only. They cannot be modified.

How can I use Session in cakephp 4?

To use Cache based sessions you can configure you Session config like: Configure::write('Session', [ 'defaults' => 'cache', 'handler' => [ 'config' => 'session' ] ]);

How to destroy session in CakePHP?

To destroy a session, use the destroy() method: $session->destroy(); Destroying a session will remove all serverside data in the session, but will not remove the session cookie.


3 Answers

//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.

like image 184
Alexander Morland Avatar answered Oct 08 '22 22:10

Alexander Morland


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.

like image 20
neilcrookes Avatar answered Oct 08 '22 22:10

neilcrookes


after save

Use Like this

$this->Session->write('Auth.User.mmid', $kinde['Kindle']['id']);
like image 32
Gowthaman D Avatar answered Oct 09 '22 00:10

Gowthaman D