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.
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.
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.
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).
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.
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.
# 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