Notice

The Notice class is used to set flash messages or notifications. These messages are displayed once and then removed after display. Available methods are:

The following keywords should be noted:

keywords
                                            
  key : a unique id string for a notice
  message : a string of text stored for displayed
                                            
                                        

Initializing Notice class
The notice class can be easily initialized as shown below.

Sample: Initializing Hasher
  $notice  = new Notice;
                                        

hasFlash
This method is used to a check if a flash key exists.

Syntax: hasFlash
  $notice->hasFlash(key); // returns true or false
                                    

Example: checking flash
    $notice->setFlash('greeting', "Welcome"); // set a notice message

    if($notice->hasFlash('greeting'))) {

        echo 'Greeting exists';

    }
                                

getFlash
This method is used to fetch a previously defined flash key. It throws an error if the flash key supplied does not exist.

Syntax: getFlash
  $notice->getFlash(key); 
                                        

Example: setFlash
  $notice->setFlash('greeting', 'Welcome to our site')

  $message = $notice->getFlash('greeting');  

  var_dump( $message ); // Welcome to our site
                                        

Example: setFlash (Real Application)
  $db = (new DB())->openDB(); //open a database class.

  $db->insert_into('users', ['username' => 'foo']);

  if( $db->insert() ){

      $notice->setFlash('notice', 'message stored successfully');
      redirect('display');

  }
    
Check flash in display page
if($notice->hasFlash('notice')){ echo $notice->getFlash('notice'); } else { echo redirect('insert'); }
In the above, when a data is successfully stored using setFlash(), it redirects to "display" url which displays the last message set. If no message is found, then a redirection will be made back to the "insert" page.

flash
Unlike the getFlash() method, flash() returns the value of an existing or non-existing key without throwing an error. This means that if a key does not exist, a null value is returned instead. .

Example: flash
  $notice->setFlash('text','1234');

  echo $notice->flash('text'); //returns 1234
  echo $notice->flash('word'); //returns null
                                    
The Resource class Res only has three static relative methods to access the notice class. These methods are Res::setFlash() Res::hasFlash() and Res::flash() which resolves similarly to the corresponding notice class methods.