Giter Club home page Giter Club logo

flash-messages's Introduction

Flash Messages

This repository contains a Flash messages service provider. This enables you to define transient messages that persist only from the current request to the next request.

Install

Via Composer

$ composer require firehead996/flash-messages

Usage

Slim 4

This example assumes that you have php-di/php-di installed.

<?php

use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteContext;
use Firehead996\Flash\MessagesInterface;
use Firehead996\Flash\Messages;

require_once __DIR__ . '/../vendor/autoload.php';

$containerBuilder = new ContainerBuilder();

// Add container definition for the flash component
$containerBuilder->addDefinitions(
    [
        MessagesInterface::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();

Please note that a message could be a string, object or array. Please check what your storage can handle.

Testing

$ phpunit

License

The MIT License (MIT). Please see License File for more information.

flash-messages's People

Contributors

akrabat avatar geggleto avatar silentworks avatar codeguy avatar designermonkey avatar firehead996 avatar l0gicgate avatar timwattenberg avatar acinader avatar bcremer avatar odan avatar joebengalen avatar rickycheers avatar tflight avatar thinksalot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.