PHP code example of williamsampaio / slim-flash-messages
1. Go to this page and download the library: Download williamsampaio/slim-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/ */
williamsampaio / slim-flash-messages example snippets
// app/dependencies.php
//...
use SlimFlashMessages\Flash;
use SlimFlashMessages\FlashProviderInterface;
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
//...
FlashProviderInterface::class => function () {
return Flash::getInstance();
},
]);
};
// app/middleware.php
//...
// use SlimFlashMessages\FlashMiddleware;
use SlimFlashMessages\FlashTwigExtension;
return function (App $app) {
//...
// Optional if you are working with dependency injection,
// using the middleware is only useful if you need to obtain the Flash instance from request.
// $app->add(FlashMiddleware::createFromContainer($app));
// With Twig
$twig = Twig::create(__DIR__ . '/../templates', ['cache' => false]);
$twig->addExtension(FlashTwigExtension::createFromContainer($app));
$app->add(TwigMiddleware::create($app, $twig));
};
// Your controller
//...
use Slim\Views\Twig;
// use SlimFlashMessages\FlashProvider;
use SlimFlashMessages\FlashProviderInterface;
class YourController
{
private $flash;
private $view;
public function __construct(FlashProviderInterface $flash, Twig $view)
{
$this->flash = $flash;
$this->view = $view;
}
public function index(ServerRequestInterface $request, ResponseInterface $response)
{
// If you are working with middleware instead of dependency injection it will be this way.
// $flash = FlashProvider::fromRequest($request);
$this->flash->add('messages', 'Hello!');
return $this->view->render($response, 'template.twig');
}
//...
}
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use SlimFlashMessages\Flash;
use SlimFlashMessages\FlashMiddleware;
use SlimFlashMessages\FlashProvider;
lashMiddleware($flash));
$app->addErrorMiddleware(true, true, true);
$app->get('/', function (Request $request, Response $response, $args) {
// Get FlashProvider from request
// FlashMiddleware previously took care of adding the FlashProvider to the request
$flash = FlashProvider::fromRequest($request);
// Clear all stored values
$flash->clearAll();
// The 'add' method allows you to add a flash message or data (as an array, if you prefer!)
$flash->add('simple', 'Hello World! 1');
$flash->add('messages', [
'status' => 'success',
'text' => '1. PHP is the best!'
]);
echo '<pre>';
var_dump($flash->getAll());
// Checks if the key is defined in the storage
var_dump($flash->has('messages'));
// Clear a key defined
$flash->clear('messages');
var_dump($flash->getAll());
var_dump($flash->has('messages'));
$flash->add('simple', 'Hello World! 2');
$flash->add('simple', 'Hello World! 3');
var_dump($flash->getAll());
// Get first item from key
var_dump($flash->get_first('simple'));
// or to pick up and remove first item.
// var_dump($flash->get_first('simple', true));
// Get last item from key
// var_dump($flash->get_last('simple'));
// or to pick up and remove last item.
var_dump($flash->get_last('simple', true));
var_dump($flash->get('simple'));
return $response;
});
$app->run();
use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use SlimFlashMessages\Flash;
use SlimFlashMessages\FlashMiddleware;
use SlimFlashMessages\FlashProvider;
use SlimFlashMessages\FlashProviderInterface;
use SlimFlashMessages\FlashTwigExtension;
ate();
$app->setBasePath('/example2'); // Optional
// Add FlashMiddleware from container
$app->add(FlashMiddleware::createFromContainer($app));
// Create Twig and add FlashTwigExtension
$twig = Twig::create(__DIR__ . '/templates', ['cache' => false]);
$twig->addExtension(FlashTwigExtension::createFromContainer($app));
// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));
$app->addErrorMiddleware(true, true, true);
$app->get('/', function (Request $request, Response $response, $args) {
// Get Twig and FlashProvider from request
$view = Twig::fromRequest($request);
// FlashMiddleware previously took care of adding the FlashProvider to the request
$flash = FlashProvider::fromRequest($request, 'flash');
$alerts = ['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'];
// The 'add' method allows you to add a flash message or data (as an array, if you prefer!)
$flash->add('simple', 'Hello World!');
$flash->add('messages', [
'alert' => $alerts[array_rand($alerts)],
'text' => '1. PHP is the best!'
]);
$flash->add('messages', [
'alert' => $alerts[array_rand($alerts)],
'text' => '2. Slim Framework is amazing!'
]);
$flash->add('messages', [
'alert' => $alerts[array_rand($alerts)],
'text' => '3. Lorem ipsum!'
]);
return $view->render($response, 'template.html.twig', [
'page' => 'Slim Flash Messages',
]);
});
$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.