Chapter 7. Session

The session plugin provides two things: automatically starts and ends the session and a flash messages mechanism. Flash messages are messages which are available only once and to the next request. This allows to pass error or success messages for example.

To create a flash message call the Atomik_Session::flash() method. It takes as first parameter the message. Messages can also have labels. For example error or success. To specify a label, use the second argument. The default label is default.

Example 7.1. Creating flash messages

			
Atomik_Session::flash('The action has completed successfully');
Atomik_Session::flash('The action has completed successfully', 'success'); // with a label
		

Note

It is possible to add a message to the current request instead of the next one by passing true as the third argument.

Flash messages can then be retreived using Atomik_Session::getMessages(). This will retrieve all messages whatever the label. The returned array contains as keys the label names and the associated values are arrays of messages.

You can also retreive one message only using Atomik_Session::getNextMessage(). This method will return an array with the first item beign the label and the second the message.

Both methods can take as argument a label name to only retreive a message from a specific label. In this case, an array of messages is returned for the first method and the message as a string for the second.

Example 7.2. Retreiving flash messages

			
foreach (Atomik::getMessages() as $label => $messages) {
	foreach ($messages as $message) {
		// ...
	}
}

list($label, $message) = Atomik::getNextMessage();
		

Finally, the Session plugin also adds a selector with the session namespace. It provides the same way of accessing arrays as Atomik::get().

Example 7.3. The session selector

			
$_SESSION['user'] = array('name' => 'paul', 'age' => 20);
A('session:user/name'); // paul
		

The plugin also adds a reference to the $_SESSION variable under the session condifuration key.

Example 7.4. Accessing $_SESSION from the global store

			
Atomik::set('session/user', array('name' => 'paul', 'age' => 20);
A('session/user/name'); // paul