Como contraparte natural al Componente Session, el Helper Session refleja la mayoría de las funcionalidades de los componentes y las hace disponible a las vistas. El Helper de Sesión se agrega automáticamente a tu vista, no es necesario agregarlo en el arreglo $helpers en el controlador.

La mayor diferencia entre el Helper y el Componente de Sesión es que el Helper no puede escribir en la sesión.

Al igual que en el Componente de Sesión, los datos son escritos y leídos usando estructuras de arreglos separadas por puntos.

array('User' =>
        array('username' => '[email protected]')

Dada la estructura de arreglo previa, el nodo sería accesado por User.username, con el punto indicando el arreglo enlazado. Esta notación es usada por todos los métodos del Helper de Sesión siempre que se utilice $key.



Read from the Session. Returns a string or array depending on the contents of the session.


Returns the current session ID.


Check to see if a key is in the Session. Returns a boolean on the key’s existence.


This will echo the contents of the $_SESSION.Message. It is used in conjunction with the Session Component’s setFlash() method.


Returns the last error in the session if one exists.


The flash method uses the default key set by setFlash(). You can also retrieve specific keys in the session. For example, the Auth component sets all of its Session messages under the “auth” key

// Controller code
$this->Session->setFlash('My Message');

// In view
// outputs "<div id='flashMessage' class='message'>My Message</div>"

// output the AuthComponent Session message, if set.

Using Flash for Success and Failure

In some web sites, particularly administration backoffice web applications it is often expected that the result of an operation requested by the user has associated feedback as to whether the operation succeeded or not. This is a classic usage for the flash mechanism since we only want to show the user the result once and not keep the message.

One way to achieve this is to use Session->flash() with the layout parameter. With the layout parameter we can be in control of the resultant html for the message.

In the controller you might typically have code:

if ($user_was_deleted) {
    $this->Session->setFlash('The user was deleted successfully.', 'flash_success');
} else {
    $this->Session->setFlash('The user could not be deleted.', 'flash_failure');

The flash_success and flash_failure parameter represents a layout file to place in the root app/views/layouts folder, e.g. app/views/layouts/flash_success.ctp, app/views/layouts/flash_failure.ctp

Inside the flash_success layout file would be something like this:

echo "<div class=\"flash flash_success\">{$content_for_layout}</div>";

The final step is in your main view file where the result is to be displayed to add simply

<?php $session->flash(); ?>

And of course you can then add to your CSS a selector for div.flash, div.flash_success and div.flash_failure