usage.md 2.27 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7
# Session Usage

## Contents

- [Storing Items](#put)
- [Retrieving Items](#get)
- [Removing Items](#forget)
8
- [Flashing Items](#flash)
Taylor Otwell committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
- [Regeneration](#regeneration)

<a name="put"></a>
## Storing Items

To store items in the session call the put method on the Session class:

	Session::put('name', 'Taylor');

The first parameter is the **key** to the session item. You will use this key to retrieve the item from the session. The second parameter is the **value** of the item.

<a name="get"></a>
## Retrieving Items

You can use the **get** method on the Session class to retrieve any item in the session, including flash data. Just pass the key of the item you wish to retrieve:

	$name = Session::get('name');

By default, NULL will be returned if the session item does not exist. However, you may pass a default value as a second parameter to the get method:

	$name = Session::get('name', 'Fred');

	$name = Session::get('name', function() {return 'Fred';});

Now, "Fred" will be returned if the "name" item does not exist in the session.

Laravel even provides a simple way to determine if a session item exists using the **has** method:

	if (Session::has('name'))
	{
	     $name = Session::get('name');
	}

<a name="forget"></a>
## Removing Items

To remove an item from the session use the **forget** method on the Session class:

	Session::forget('name');

You can even remove all of the items from the session using the **flush** method:

	Session::flush();

53 54 55 56 57 58 59
<a name="flash"></a>
## Flashing Items

The **flash** method stores an item in the session that will expire after the next request. It's useful for storing temporary data like status or error messages:

	Session::flash('status', 'Welcome Back!');
	
TommyC81 committed
60
Flash items that are expiring in subsequent requests can be retained for another request by using one of the **reflash** or **keep** methods:
61 62 63 64 65 66 67 68 69 70 71 72 73

Retain all items for another request:

	Session::reflash();
	
Retain an individual item for another request:
	
	Session::keep('status');
	
Retain several items for another request:
	
	Session::keep(array('status', 'other_item'));

Taylor Otwell committed
74 75 76 77 78 79
<a name="regeneration"></a>
## Regeneration

Sometimes you may want to "regenerate" the session ID. This simply means that a new, random session ID will be assigned to the session. Here's how to do it:

	Session::regenerate();