Session
Since HTTP driven applications are stateless, sessions provide a way to store information about the user across multiple requests. Atom ships with a variety of session backends that are accessed through an expressive, unified API. By default, sessions are stored in storage/framework/sessions
.
Using The Session
There are two primary ways of working with session data in Atom: the global session
helper and via a Request instance. First, let's look at accessing the session via a Request instance:
When you retrieve an item 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 and the requested key does not exist, the Closure
will be executed and its result returned:
The Global Session Helper
You may also use the global 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:
Retrieving All Session Data
If you would like to retrieve all the data in the session, you may use the all
method:
Determining If An Item Exists In The Session
To determine if an item is present in the session, you may use the has
method. The has
method returns true
if the item is present and is not null
:
To determine if an item is present in the session, even if its value is null
, you may use the exists
method. The exists
method returns true
if the item is present:
Storing Data
To store data in the session, you will typically use the put
method or the session
helper:
Pushing To Array Session Values
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:
Retrieving And Deleting An Item
The pull
method will retrieve and delete an item from the session in a single statement:
Deleting Data
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:
Regenerating The Session ID
Dernière mise à jour
Cet article vous a-t-il été utile ?