Multidimensional session

36 views
Skip to first unread message

Adri Kodde

unread,
May 8, 2012, 4:46:11 PM5/8/12
to Alloy
Is it possible to store and retrieve multidimensional arrays with the
session class?

For example:
$this->kernel->session()->set('backend', array('id' => 2);

This would give some issues in altering and retrieving the data, right?

Vance Lucas

unread,
May 8, 2012, 5:19:49 PM5/8/12
to allo...@googlegroups.com
Yes, it should be possible. It's just a thin wrapper around native PHP sessions, which serialize the data when the session is written and unserialize the data when the session is loaded.

--
Vance Lucas
http://vancelucas.com

Adri Kodde

unread,
May 9, 2012, 3:25:00 AM5/9/12
to allo...@googlegroups.com
OK, I've tried:

var_dump($this->kernel->session()->get('backend'));
this outputs: array(1) { ["name"]=> int(1) }

I changed the session class function so it only outputs (objects) (by typecasting).
Now I can access the name variable by:
$this->kernel->session()->get('backend')->name

Is this a good solution, you think, or can it be simplified?

By the way, great project! I more and more get to know the way it works. Thanks!

8 mei 2012 23:19
Yes, it should be possible. It's just a thin wrapper around native PHP sessions, which serialize the data when the session is written and unserialize the data when the session is loaded.

--
Vance Lucas
http://vancelucas.com




8 mei 2012 22:46

Vance Lucas

unread,
May 9, 2012, 10:30:12 AM5/9/12
to allo...@googlegroups.com
It looks like it's outputting an array, so you'll have to do this:

$backend = $this->kernel->session()->get('backend');
var_dump($backend['name']);

If you're lucky enough to have PHP 5.4, you should be able to do this (array de-referencing):

var_dump($this->kernel->session()->get('backend')['name']);

Eventually I want to be able to support dot-notation stuff like this in the Alloy session driver, but it is not currently supported. It is supported in the Request object though for accessing nested POST/GET data, so the code is in Alloy already:

var_dump($this->kernel->session()->get('backend.name');

Regards,
postbox-contact.jpg
postbox-contact.jpg

Adri Kodde

unread,
May 9, 2012, 11:15:03 AM5/9/12
to allo...@googlegroups.com
Thanks for your response. I've changed my code so it now uses the global session variable instead. The whole setting / getting with the PHP magic methods were causing me too much trouble :)

I've found some threads on google, but haven't found a good solution yet.

Cheers, Adri

9 mei 2012 16:30
It looks like it's outputting an array, so you'll have to do this:

$backend = $this->kernel->session()->get('backend');
var_dump($backend['name']);

If you're lucky enough to have PHP 5.4, you should be able to do this (array de-referencing):

var_dump($this->kernel->session()->get('backend')['name']);

Eventually I want to be able to support dot-notation stuff like this in the Alloy session driver, but it is not currently supported. It is supported in the Request object though for accessing nested POST/GET data, so the code is in Alloy already:

var_dump($this->kernel->session()->get('backend.name');

Regards,
9 mei 2012 09:25
Reply all
Reply to author
Forward
0 new messages