N/APosted on - 08/27/2011
I have one confusion about cakephp's helper. Cakephp have two type of session helper:
Both are use for same purpose of to navigating with session value.
So Question is Why should we use two different Session helper for one same task? Is it against MVC structure?
CakePhp Session Helper – Session Component Problem
is a PHP framework which is turning out to be most useful, and very flexible. I've embarked on a few little projects with it, and thought I would spread a little understanding with regards to the session component – as I struggled to find a concise set of examples to help me on my way.
By default, a CakePHP application will automatically create a session instance when you're browsing through it – this is good, but it's not that obvious about where to go after this. If you were to put the following code into a controller:
print_r($this -> Session -> read());
The contents of the session variable would be printed out (print_r() dumps the contents of an array to screen). Without any influence over the session data, it would look like this:
Array ( [Config] => Array ( [rand] => 262820453 [time] => 1161876896 [userAgent] => c7f575cbe5a4b7ad0efb748d54124611 ) )
(Bear in mind that the numbers & characters will all be different for you.)
If you are looking to disable this default session behavior, set the AUTO_SESSION constant to false in
Writing a variable into the session.
Anyway, let's get onto some more interesting subject stuff – making the Session component work for us. If you want to write one value into the session, it's merely a case of doing:
$this -> Session -> write("variable", "value");
If you look at a dump of the session array
(print_r($this -> Session -> read()))
You will see that right at the end of the array, there's the text
"[variable] => value" – magic.
Reading a variable from the session
If you want to use this session variable anywhere in your application, you can simply do:
$this -> Session -> read("variable");
This will return the string "value".
Putting objects into the session
Handily, you can store entire objects in the Session component – useful for passing information backwards and forwards regarding a user account/profile or the contents of a shopping cart. All you need to do is change the "value" part of the above example to be an array instead of a standard string value. For example:
$user_email = $this -> data['User']['email'];
$user = $this -> User -> find("email = '$user_email'");
$this -> Session -> write('User', $user['User']);
In the above working example we expect a form to have been submitted to the controller containing a "User/email" value – we then look in the Users database table to find a row with the given e-mail address.
After we've found it, we store it into the Session component. Please note the $user['User'] is not the entire array as we only want the user information – there may well be other rows returned by CakePHP depending on your model associations (e. g. A Profile could belong to a User, and our database query above would return the Profile data too.).
Considering the above example, how would we get specific data out of the Session component? Easy, but not obvious:
$this -> Session -> read("User.email");
Closing/destroying a session.
Most uses of destroying a session will for logging users out of a system – and it's made very easy. For neatness it's better to check and make sure the session is still valid before attempting to destroy it:
if ($this -> Session -> valid())
$this -> Session -> destroy();
$this -> redirect('/');
It's also courteous to redirect the user somewhere useful.