config/session.php
. Be sure to review the options available to you in this file. By default, Laravel is configured to use the file
session driver, which will work well for many applications.driver
configuration option defines where session data will be stored for each request. Laravel ships with several great drivers out of the box:file
- sessions are stored in storage/framework/sessions
.cookie
- sessions are stored in secure, encrypted cookies.database
- sessions are stored in a relational database.memcached
/ redis
- sessions are stored in one of these fast, cache based stores.array
- sessions are stored in a PHP array and will not be persisted.database
session driver, you will need to create a table to contain the session items. Below is an example Schema
declaration for the table:session:table
Artisan command to generate this migration:predis/predis
package (~1.0) via Composer. For more information on configuring Redis, consult its Laravel documentation page.session
configuration file, the connection
option may be used to specify which Redis connection is used by the session.session
helper and via a Request
instance. First, let's look at accessing the session via a Request
instance, which can be type-hinted on a controller method. Remember, controller method dependencies are automatically injected via the Laravel service container: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 and the requested key does not exist, the Closure
will be executed and its result returned:session
PHP function to retrieve and store data in the session. When the session
helper is called with a single, string argument, it will return the value of that session key. When the helper is called with an array of key / value pairs, those values will be stored in the session:session
helper. Both methods are testable via the assertSessionHas
method which is available in all of your test cases.all
method:has
method. The has
method returns true
if the item is present and is not null
:null
, you may use the exists
method. The exists
method returns true
if the item is present:put
method or the session
helper: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:pull
method will retrieve and delete an item from the session in a single statement:flash
method. Data stored in the session using this method will be available immediately and during the subsequent HTTP request. After the subsequent HTTP request, the flashed data will be deleted. Flash data is primarily useful for short-lived status messages:reflash
method, which will keep all of the flash data for an additional request. If you only need to keep specific flash data, you may use the keep
method: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:LoginController
; however, if you need to manually regenerate the session ID, you may use the regenerate
method.SessionHandlerInterface
. This interface contains just a few simple methods we need to implement. A stubbed MongoDB implementation looks something like this:Extensions
directory to house the MongoSessionHandler
.open
method would typically be used in file based session store systems. Since Laravel ships with a file
session driver, you will almost never need to put anything in this method. You can leave it as an empty stub. It is a fact of poor interface design (which we'll discuss later) that PHP requires us to implement this method.close
method, like the open
method, can also usually be disregarded. For most drivers, it is not needed.read
method should return the string version of the session data associated with the given $sessionId
. There is no need to do any serialization or other encoding when retrieving or storing session data in your driver, as Laravel will perform the serialization for you.write
method should write the given $data
string associated with the $sessionId
to some persistent storage system, such as MongoDB, Dynamo, etc. Again, you should not perform any serialization - Laravel will have already handled that for you.destroy
method should remove the data associated with the $sessionId
from persistent storage.gc
method should destroy all session data that is older than the given $lifetime
, which is a UNIX timestamp. For self-expiring systems like Memcached and Redis, this method may be left empty.extend
method on the Session
facade. You should call the extend
method from the boot
method of a service provider. You may do this from the existing AppServiceProvider
or create an entirely new provider:mongo
driver in your config/session.php
configuration file.