PHP code example of nassiry / flash-messages

1. Go to this page and download the library: Download nassiry/flash-messages library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nassiry / flash-messages example snippets




use Nassiry\FlashMessages\FlashMessages;

// Create an instance
$flash = FlashMessages::create();

// Add flash messages to be displayed on the next page load
$flash->success('Operation successful!');
$flash->error('An error occurred.');

// Render messages on the next page template file
$flash->render();



use Nassiry\FlashMessages\FlashMessages;

// Create an instance
$flash = FlashMessages::create();

// Add flash messages to be displayed immediately
$flash->error('An error occurred.', true);

// Render messages instantly in the same page
$flash->render();

// Check if ANY messages exist
if ($flash->hasMessages()) {
    echo "There are flash messages available.";
}

// Check if SPECIFIC TYPE messages exist
if ($flash->hasMessages('info')) {
    echo "There are info messages available.";
}

// Get ALL messages
$allMessages = $flash->getMessages();
foreach ($allMessages as $message) {
    echo $message['type'] . ': ' . $message['message'] . "<br>";
}

// Get messages of SPECIFIC TYPE
$infoMessages = $flash->getMessages('info');
if (!empty($infoMessages)) {
    foreach ($infoMessages as $message) {
        echo 'Info: ' . $message['message'] . "<br>";
    }
} else {
    echo "No info messages found.";
}

$flash->render();

$flash->clear();

$flash->addCustomType('notification',
 'This is a custom notification message!',
  true
 );
$flash->addCustomType('alert',
 'This is an alert message!',
  false
);

use Nassiry\FlashMessages\FlashMessageRenderer;

class CustomRenderer extends FlashMessageRenderer
{
    public function renderMessage(string $type, string $message): void
    {
        echo sprintf('<div class="custom-%s">%s</div>', htmlspecialchars($type), htmlspecialchars($message));
    }
}

$renderer = new CustomRenderer();
$flash = new FlashMessages(new FlashMessageStorage(), $renderer);
$flash->success('Custom rendered message!');
$flash->render();