1. Go to this page and download the library: Download firehead996/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/ */
firehead996 / flash-messages example snippets
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteContext;
use Firehead996\Flash\MessagesInterface;
use Firehead996\Flash\Messages;
:class => function () {
$storage = [];
return new Messages($storage);
}
]
);
AppFactory::setContainer($containerBuilder->build());
$app = AppFactory::create();
// Add session start middleware
$app->add(function ($request, $next) {
// Start PHP session
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
// Change flash message storage
$this->get(MessagesInterface::class)->__construct($_SESSION);
return $next->handle($request);
});
$app->addErrorMiddleware(true, true, true);
// Add routes
$app->get('/', function ($request, $response) {
// Set flash message for next request
$this->get(MessagesInterface::class)->addMessage('Test', 'This is a message');
// Redirect
$url = RouteContext::fromRequest($request)->getRouteParser()->urlFor('bar');
return $response->withStatus(302)->withHeader('Location', $url);
});
$app->get('/bar', function ($request, $response) {
$flash = $this->get(MessagesInterface::class);
// Get flash messages from previous request
$messages = $flash->getMessages();
print_r($messages);
// Get the first message from a specific key
$test = $flash->getFirstMessage('Test');
print_r($test);
return $response;
})->setName('bar');
$app->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.