Documentation
Session Manager
Configuration
You may configure session related parameters stored in src/Apps/Configs/session.php file. By default cygnite uses PHP native session to store data, you can also make use of database based session simply changing the driver configuration.
Usage: Including Namespace
use Cygnite\Common\SessionManager\Session;
Storing Into Session
Storing a value into session is just simple. Example given below.
Session::set('name', 'Peter'); // Will store the name into session
You can specify the driver in src/Apps/Configs/session.php. By default session manager connects with native session, you may also use database based session storage.You just need to change the driver in the array as "native" or "database".
Retrieving A Value From Session
echo Session::get('name'); // Will Display Peter
Checking Existence Of Value In Session
Check if key exists into session as below.
if (Session::has('name')) {
echo Session::get('name');
}
Removing A Value From Session
Remove a particular value from session
Session::delete('name');
Destroy All Session Values
Session::delete();
Setting Flash Message
You may wish to set some flash message before redirecting to another page, and to display once redirecting to the landing page. You can simply make use of flash message. Flash message is useful for processing a form, redirect and to show a special message on the next request. Below example usage flash message.
use Apps\Models\Product;
$product = new Product();
$product->name = 'Cygnite';
$product->description = 'Awesome Product';
if ($product->save()) {
$this->setFlash('success', 'Product saved successfully!')
->redirectTo('product/index/'.Url::segment(4));
} else {
$this->setFlash('error', 'Error occurred while saving product!')
->redirectTo('product/index/'.Url::segment(4));
}
In the above example we are storing data into database, set flash message and then redirect to another page. You can retrieve the flash message in next page as below.
Rendering Flash Message
In the view page for the next action below code used to check notice message exists and then display to user.
if ($this->hasFlash('success')) {
echo $this->getFlash('success');
} else if ($this->hasError()) {
echo $this->getFlash('error');
}