PHP code example of sayful1 / slim-flash

1. Go to this page and download the library: Download sayful1/slim-flash 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/ */

    

sayful1 / slim-flash example snippets


// Start PHP session
session_start();

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['flash'] = function () {
    return new \Sayful\SlimFlash\Flash();
};

$app->get('/foo', function ($req, $res, $args) {
    // Set flash message for next request
    $this->flash->success('Test', 'This is a message');
    
    // You may also do
    $this->flash->info('Info!', 'This is info message.');
    $this->flash->warning('Warning!', 'This is warning message.');
    $this->flash->error('Title', 'This is error message.');
    
    // All method can also take on parameter
    $this->flash->success('This is message without title.');

    // Redirect
    return $res->withStatus(302)->withHeader('Location', '/bar');
});

$app->get('/bar', function ($req, $res, $args) {
    // Get flash messages from previous request
    $messages = $this->flash->getMessages();
    print_r($messages);

    // Get the first message
    $test = $this->flash->getFlashMessage();
    print_r($test);
});

$app->run();

$container['view'] = function ($container) {
    ...
    // Add flash as global variable to twig view
    $view->getEnvironment()->addGlobal( 'flash', $container->flash );
    ...
};