added documentation to main repository.

This commit is contained in:
Taylor Otwell
2011-07-15 19:25:29 -05:00
parent cec583aafe
commit bfaf6950c6
21 changed files with 2392 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<a name="config"></a>
## Session Configuration
- [File System Sessions](#file)
- [Database Sessions](#database)
- [Memcached Sessions](#memcached)
The web is a stateless environment. This means that each request to your application is considered unrelated to any previous request. However, **sessions** allow you to store arbitrary data for each visitor to your application. The session data for each visitor is stored on your web server, while a cookie containing a **session ID** is stored on the visitor's machine. This cookie allows your application to "remember" the session for that user and retrieve their session data on subsequent requests to your application.
Sound complicated? If so, don't worry about it. Just tell Laravel where to store the sessions and it will take care of the rest.
Three great session drivers are available out of the box:
- File System
- Database
- Memcached
<a name="file"></a>
### File System Sessions
Most likely, your application will work great using file system sessions. However, if your application receives heavy traffic or runs on a server farm, use database or Memcached sessions.
To get started using file system sessions, just set the driver option in the **application/config/session.php** file:
'driver' => 'file'
That's it. You're ready to go!
> **Note:** File system sessions are stored in the **application/storage/sessions** directory, so make sure it's writeable.
<a name="database"></a>
### Database Sessions
To start using database sessions, you will first need to [configure your database connection](/docs/database/config).
Already setup your database? Nice! Next, you will need to create a session table. Here are some SQL statements to help you get started:
#### SQLite
CREATE TABLE "sessions" (
"id" VARCHAR PRIMARY KEY NOT NULL UNIQUE,
"last_activity" INTEGER NOT NULL,
"data" TEXT NOT NULL
);
#### MySQL
CREATE TABLE `sessions` (
`id` VARCHAR(40) NOT NULL,
`last_activity` INT(10) NOT NULL,
`data` TEXT NOT NULL,
PRIMARY KEY (`id`)
);
If you would like to use a different table name, simply change the **table** option in the **application/config/session.php** file:
'table' => 'sessions'
Great! All you need to do now is set the driver in the **application/config/session.php** file:
'driver' => 'db'
<a name="memcached"></a>
### Memcached Sessions
Before using Memcached sessions, you must [configure your Memcached servers](/docs/cache/config#memcached).
All done? Great! Just set the driver in the **application/config/session.php** file:
'driver' => 'memcached'

View File

@@ -0,0 +1,59 @@
## Session Usage
- [Storing Items](#put)
- [Retrieving Items](#get)
- [Removing Items](#forget)
- [Regeneration](#regeneration)
<a name="put"></a>
### Storing Items
Storing items in the session is a breeze. Simply 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.
Need to store an item in the session that should expire after the next request? Check out the **flash** method. It provides an easy way to store temporary data like status or error messages:
Session::flash('status', 'Welcome Back!');
<a name="get"></a>
### Retrieving Items
Retrieving items from the session is no problem. 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
Need to get rid of a session item? No problem. Just mention the name of the item to 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();
<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();