Since HTTP driven applications are stateless, sessions provide a way to store information about the user across requests. October ships with a variety of session back-ends available for use through a clean, unified API. Support for popular back-ends such as Memcached, Redis, and databases is included out of the box.
The session configuration is stored in config/session.php
. Be sure to review the well documented options available to you in this file. By default, October is configured to use the file
session driver, which will work well for the majority of applications.
Note: The array driver is typically used for running unit tests to prevent session data from persisting.
October uses the flash
session key internally, so you should not add an item to the session by that name.
Using the Session
facade you may call a variety of functions to interact with the underlying data. For example, the put
method stores a new piece of data in the session:
Session::put('key', 'value');
The push
method may be used to push a new value onto a session value that is an array. For example, if the user.teams
key contains an array of team names, you may push a new value onto the array like so:
Session::push('user.teams', 'developers');
When you retrieve a value from the session, you may also pass a default value as the second argument to the get
method. This default value will be returned if the specified key does not exist in the session. If you pass a Closure
as the default value to the get
method, the Closure
will be executed and its result returned:
$value = Session::get('key');
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });
If you would like to retrieve all data from the session, you may use the all
method:
$data = Session::all();
The pull
method will retrieve and delete an item from the session:
$value = Session::pull('key', 'default');
The has
method may be used to check if an item exists in the session. This method will return true
if the item exists:
if (Session::has('users')) {
//
}
The forget
method will remove a piece of data from the session. If you would like to remove all data from the session, you may use the flush
method:
Session::forget('key');
Session::flush();
If you need to regenerate the session ID, you may use the regenerate
method:
Session::regenerate();
Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash
method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:
Session::flash('key', 'value');
If you need to keep your flash data around for even more requests, you may use the reflash
method, which will keep all of the flash data around for an additional request. If you only need to keep specific flash data around, you may use the keep
method:
Session::reflash();
Session::keep(['username', 'email']);