Skip to main content

Flash Message

https://packagist.org/packages/tamtamchik/simple-flash

simple-flash

Install

Via Composer.
$ composer require tamtamchik/simple-flash
Inside your project make sure to start a session and load Composer autoload to make everything work.
<?php
// Start a Session
if( !session_id() ) @session_start();

// Initialize Composer Autoload
require_once 'vendor/autoload.php';
Warning! This library contains global flash() function, that potentially can break your function with this name. Now you are warned!

Usage

There are 3 ways to use library:
use \Tamtamchik\SimpleFlash\Flash;

// Instance
$flash = new Flash();
$flash->message('Tea.');

// Static
Flash::message('Earl Gray.');

// Function
flash()->message('Hot!');
Messages added by calling message($message, $type = 'info') method. In case of calling a function flash() you can pass $message, $type just to function like so:flash('resistance is futile').
Because any of creation types return \Tamtamchik\SimpleFlash\Flash instance, so you can always use chaining to add multiple messages. Shortcuts available for all types of base message types, also you can pass arrays as $message.
flash()->error(['Invalid email!', 'Invalid username!'])
       ->warning('Warning message.')
       ->info('Info message.')
       ->success('Success message!');
Out of the box library support 4 different types of messages: errorwarninginfosuccess. So far output is hard coded and designed for Bootstrap.
<div class="alert alert-danger" role="alert">
  <p>Invalid email!</p>
  <p>Invalid username!</p>
</div>
<div class="alert alert-warning" role="alert"><p>Warning message.</p></div>
<div class="alert alert-info" role="alert"><p>Info message.</p></div>
<div class="alert alert-success" role="alert"><p>Success message!</p></div>
Rendering is simple:
// Rendering specific type
$output = flash()->display('error');

// Rendering all flash
$output = flash()->display();

// Also rendering possible when you just read instance of \Tamtamchik\SimpleFlash\Flash object as a string
(string) flash();

// or ... it's totally just for display, never do this in real life...
<?php
// ... some code
$flash = new Flash();
$flash->warning('It is totally just for display, never do this in real life...');
// ... some other code
?>
<!-- ... some html -->
<div class="flashes">
  <?= $flash; ?>
</div>
<!-- ... some other html -->

Templates

Template Factory

Package comes with a set of templates for most popular CSS frameworks. Bootstrap 3Bootstrap 4Foundation 5Foundation 6Semantic UI 2UIKit 2.
This templates can be created using TemplateFactory that comes with package. All templates have aliases defined in Templates.
use Tamtamchik\SimpleFlash\Flash;
use Tamtamchik\SimpleFlash\TemplateFactory;
use Tamtamchik\SimpleFlash\Templates;

// get template from factory, e.g. template for Foundation 6
$template = TemplateFactory::create(Templates::FOUNDATION_6);

// passing template via function
flash('Info message using Foundation 6 template!', 'info', $template);

// passing to constructor
$flash = new Flash($template);

// using setTemplate function
$flash->setTemplate($template);

Creating own templates

Template is basically any class that implements TemplateInterface. But to make it easy you can extend BaseTemplate, it contains most of the functions.
Defining and using this sample class as template:
use Tamtamchik\SimpleFlash\BaseTemplate;
use Tamtamchik\SimpleFlash\TemplateInterface;

class CustomTemplate extends BaseTemplate implements TemplateInterface
{
    protected $prefix  = '<li>'; // every line prefix
    protected $postfix = '</li>'; // every postfix
    protected $wrapper = '<ul class="alert-%s">%s</ul>'; // wrapper over messages of same type

    /**
     * @param $messages - message text
     * @param $type     - message type: success, info, warning, error
     *
     * @return string
     */
    public function wrapMessages($messages, $type)
    {
        return sprintf($this->getWrapper(), $type, $messages);
    }
}

flash()
    ->setTemplate(new CustomTemplate)
    ->error(['Invalid email!', 'Invalid username!'])
    ->warning('Warning message.')
    ->info('Info message.')
    ->success('Success message!')
    ->display();
Will output following:
<ul class="alert-error">
    <li>Invalid email!</li>
    <li>Invalid username!</li>
</ul>
<ul class="alert-warning">
    <li>Warning message.</li>
</ul>
<ul class="alert-info">
    <li>Info message.</li>
</ul>
<ul class="alert-success">
    <li>Success message!</li>
</ul>


Comments

Popular posts from this blog

October CMS E-Commerce Tutorial: GoT White Walkers Protection Store

As many of you probably know, Game of Thrones Season 6 is starting next April 24th. Now I recently introduced myself on this blog , but forgot to mention that I'm, like millions of others, a shameless GoT fan . When our content guy asked me to craft a post showing how the easy e-commerce integration we brag about would work with October CMS, I immediately picked GoT as a theme for the demo. ​ So in this post, I'm going to show you how to set up a store selling defense against the imminent White Walkers invasion. Because WINTER IS COMING big time, you know. ​ ​ More specifically, I'll provide a step-by-step e-commerce tutorial explaining how to integrate our shopping cart platform to an October CMS site. Let's get into it. ​ What is October CMS ​ We've been hearing about October CMS from developers here and there for a while now. This free, open-source CMS platform is the brainchild of fellow Canadian Alexey Bobkov and Australian Samuel Georges. It...

Sublime vs Dreamweaver

13 REASONS TO USE OF SUBLIME TEXT OVER DREAMWEAVER 17 MARCH, 2014 BY TOM ELLIOTT I started writing this post over a year ago but back then it was entitled something along the lines of ‘Reasons why I use Dreamweaver over code editors’. This was biased, as I hadn’t properly explored other code editors like Coda, Sublime Text or Notepad++. So, wanting to write an objective post from a web developers point of view, and because I had heard many good things about it, I setup Sublime Text 2 and dove straight in. I’m glad I did because it quickly became obvious that my presumed reasons for Dreamweaver’s superiority were just wrong. We are all creatures of habit to a greater or lesser degree and when we develop workflows to help make our lives easier, we can get entrenched in the view that these honed practices are superior. It’s not our fault, confirmation bias is supposedly  programmed in our DNA . Even so, it’s healthy to try and keep questioning and challenging ourselves, espe...

JazzCash Mobile Account

  JazzCash Mobile Account Help Center  > JazzCash Mobile Account What is JazzCash mobile account? JazzCash Mobile Account is an actual bank account that is tagged with your mobile number and can be operated through your phone. Through this Mobile Account you can enjoy complete freedom of accessing financial services anywhere, anytime! More importantly, you don’t have to rely on traveling to a Bank branch, wait at queues or complete any documentation. Mobile Account menu works on all types of mobile phones – smart phone is not required. Customers can make deposits or withdrawals through any Mobilink Microfinance Bank Branch, Mobilink Franchise, Mobilink Business Center and Jazzcash Agents spread across Pakistan. JazzCash Mobile App In line with the continuous digitization of its services to meet demands of growing number of smartphone users, JazzCash is proud to announce Android based App for its Mobile Account users. The App offers a user friendly inte...