Skip to content

Latest commit

 

History

History
 
 

session

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Session module (lmvc-modules)

This module intends abstracting from interaction with php's session ($_SESSION) within an application of the LMVC-framework.

It offers a little helper collection reading and writing to and from the session array.

Installation

Just setup your app's config.json to add the session module to the modules-array:

"modules": [
   "Scandio\\lmvc\\modules\\session"
]

You're set!

As a side note: the Boostrap.php of this module will automatically start the session for you.

Usage

Getting parameters

Parameters can be accessed via dot-notation as a parameters within the ::get()-method.

$userId = Session::get('user.id');

$userData = Session::get('user');

The last example shows that accessing a non-primitive value will just return the object at the current dot-notation's pointer position.

Settings parameters

Parameters can be set in a similar fashion as getting them.

Session::set('user.id', $user->id);

Session::set('authenticated', true);
Session::set('user.id', $userId);

$authenticated = Session::get('authenticated', true);

The first example shows setting a value while the second one outlines the ability to give a default value which will be set if the demanded value ain't set yet. If it was set the get would return its value (e.g. false).

Merging values into the session-object

It is also possible to merge a whole array into the session where its values will only be set whenever it has not been set previously.

Session::merge([
    'user' => [
        'id' => $userModel->id
    ],
    'authenticated' => Security::get()->isAuthenticated();
]);

Remember, whenever a value is found which is already set, e.g. the authenticated = false it will not be set to true even if Security::get()->isAuthenticated() returns true now.

Replacing values in the session

The last problem can be solved by using the ::replace()-method.

Session::replace([
    'user' => [
        'id' => $userModel->id
    ],
    'authenticated' => Security::get()->isAuthenticated();
]);

The above example will replace every value (set it if not existend) in the session no matter what its previous value was.

Some helper methods

# Starts a session
Session::start();

# Checks if a value is set
Session::has('user.id');

# Flushes the session's data
Session::flush();

# Regenerates the session's id - optional params are $flush and $lifetime in seconds.
Session::regenerate();

# Finally closes the session.
Session::close();

Thanks for reading