PHP Classes

Simple Flash Messages: Display message with different CSS frameworks

Recommend this page to a friend!
  Info   View files Documentation   Screenshots Screenshots   View files View files (57)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-01-27 (23 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 240 This week: 4All time: 7,997 This week: 117Up
Version License PHP version Categories
simple-flash 1.2.12Custom (specified...5.3HTML, PHP 5
Collaborate with this project Author

simple-flash - github.com

Description

This package can display message with different CSS frameworks.

It can generate HTML to display messages using classes of several types of CSS frameworks.

The messages can be either of the types: errors, warnings, information and success.

Currently it supports the CSS frameworks:

- Bootstrap 3 (default)
- Bootstrap 4
- Foundation 5
- Foundation 6
- Semantic UI 2
- Siimple
- UIKit 2

Innovation Award
PHP Programming Innovation award nominee
June 2016
Number 2


Prize: PhpStorm IDE 1 year individual subscription
Many applications need to show to their users several types of common messages like errors, warnings, information, etc..

This package can display all those types of messages using a different types of style frameworks like Bootstrap, Foundation, Semantic UI, etc..

Manuel Lemos
Picture of Yuriy Tkachenko
  Performance   Level  
Name: Yuriy Tkachenko <contact>
Classes: 2 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Details

Simple Flash Messages

[![Latest Version on Packagist][ico-version]][link-packagist] [![Total Downloads][ico-downloads]][link-downloads] [![Software License][ico-license]](LICENSE.md) [![Build Status][ico-travis]][link-travis] [![Coverage Status][ico-scrutinizer]][link-scrutinizer] [![Quality Score][ico-code-quality]][link-code-quality] [![SensioLabsInsight][ico-insight]][link-insight]

Easy, framework-agnostic flash notifications for PHP. Inspired by laracasts/flash and plasticbrain/PHP-Flash-Messages. It supports multiple CSS frameworks out of the box:

simple-flash

> The documentation below is for version 3.x! > If you are looking the documentation for version 2.x look here. > If you are looking the documentation for version 1.x look here.

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';

Usage

There are 3 ways to use library:

use \Tamtamchik\SimpleFlash\Flash;
use function 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.

use function Tamtamchik\SimpleFlash\flash;

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: error, warning, info, success.

<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:

use function Tamtamchik\SimpleFlash\flash;

// 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

Using templates you can customize how flash messages will be rendered. Package comes with a set of templates for most popular CSS frameworks:

Templates::BASE; // Same as Templates::BOOTSTRAP
Templates::BOOTSTRAP;   // https://getbootstrap.com
Templates::FOUNDATION;  // https://get.foundation
Templates::BULMA;       // https://bulma.io
Templates::MATERIALIZE; // https://materializecss.com
Templates::TAILWIND;    // https://tailwindcss.com
Templates::PRIMER;      // https://primer.style
Templates::UIKIT;       // https://getuikit.com
Templates::SEMANTIC;    // https://semantic-ui.com
Templates::SPECTRE;     // https://picturepan2.github.io/spectre
Templates::HALFMOON;    // https://www.gethalfmoon.com

Shortcuts

You cah pass template name as a second argument to display() function:

use function Tamtamchik\SimpleFlash\flash;

flash()->success('Success message!');
...
// rendering with Halfmoon template using Templates::HALFMOON as a shortcut
echo flash()->display('success', Templates::HALFMOON);

Or you can use descriptive display functions:

use function Tamtamchik\SimpleFlash\flash;

flash()->success('Success message!');
...
echo flash()->displayBootstrap();
echo flash()->displayFoundation();
echo flash()->displayBulma();
echo flash()->displayMaterialize();
echo flash()->displayTailwind();
echo flash()->displayPrimer();
echo flash()->displayUiKit();
echo flash()->displaySemantic();
echo flash()->displaySpectre();
echo flash()->displayHalfmoon();

Factory

Package comes with a set of templates for most popular CSS frameworks:

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
$template = TemplateFactory::create(Templates::FOUNDATION);

// 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 templates

Template is basically any class that implements TemplateInterface. But to make it easy you can extend BaseTemplate, it already contains most of the functions.

Defining and using this sample class as template:

use Tamtamchik\SimpleFlash\BaseTemplate;
use Tamtamchik\SimpleFlash\TemplateInterface;
use function Tamtamchik\SimpleFlash\flash;

class CustomTemplate extends BaseTemplate implements TemplateInterface
{
    protected $prefix  = '<li>'; // every line prefix
    protected $postfix = '</li>'; // every line 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>

Interface

Package provides TemplateInterface for Simple Flash templates.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer tests

Examples

$ composer examples

And then just visit http://localhost:8000

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email <yuri.tam.tkachenko@gmail.com> instead of using the issue tracker.

Credits

  • [Yuri Tkachenko][link-author]
  • [All Contributors][link-contributors]

License

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

[![Buy Me A Coffee][ico-coffee]][link-coffee]

[ico-version]: https://img.shields.io/packagist/v/tamtamchik/simple-flash.svg?style=flat-square [ico-license]: https://img.shields.io/packagist/l/tamtamchik/simple-flash.svg?style=flat-square [ico-travis]: https://img.shields.io/travis/tamtamchik/simple-flash/master.svg?style=flat-square [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/tamtamchik/simple-flash.svg?style=flat-square [ico-code-quality]: https://img.shields.io/scrutinizer/g/tamtamchik/simple-flash.svg?style=flat-square [ico-downloads]: https://img.shields.io/packagist/dt/tamtamchik/simple-flash.svg?style=flat-square [ico-coffee]: https://img.shields.io/badge/Buy%20Me%20A-Coffee-%236F4E37.svg?style=flat-square [ico-insight]: https://img.shields.io/symfony/i/grade/a92cacf2-ba32-4f36-b040-5a9f1d7f8f25.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/tamtamchik/simple-flash [link-travis]: https://app.travis-ci.com/github/tamtamchik/simple-flash [link-scrutinizer]: https://scrutinizer-ci.com/g/tamtamchik/simple-flash/code-structure [link-code-quality]: https://scrutinizer-ci.com/g/tamtamchik/simple-flash [link-downloads]: https://packagist.org/packages/tamtamchik/simple-flash [link-author]: https://github.com/tamtamchik [link-contributors]: ../../contributors [link-coffee]: https://www.buymeacoffee.com/tamtamchik [link-insight]: https://insight.symfony.com/projects/a92cacf2-ba32-4f36-b040-5a9f1d7f8f25

Screenshots  
  • demo
  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imageexamples (15 files)
Files folder imageissues (2 directories)
Files folder imagesrc (6 files, 3 directories)
Files folder imagetests (4 files)
Accessible without login Plain text file CHANGELOG.md Doc. changelog
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data composer
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Doc. conduct
Accessible without login Plain text file CONTRIBUTING.md Doc. contributing
Accessible without login Plain text file LICENSE.md Lic. license
Accessible without login Plain text file phpunit.xml Data phpunit
Accessible without login Plain text file README.md Doc. readme
Accessible without login Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (2 files)

  Files folder image Files  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file bug_report.md Data Auxiliary data
  Accessible without login Plain text file feature_request.md Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file bootstrap.php Example Example script
  Accessible without login Plain text file bulma.php Example Example script
  Accessible without login Plain text file custom.php Example Demo Custom
  Accessible without login Plain text file foundation.php Example Example script
  Accessible without login Plain text file halfmoon.php Example Example script
  Accessible without login Plain text file index.php Example Demo index
  Accessible without login Plain text file materialize.php Example Example script
  Accessible without login Plain text file primer.php Example Example script
  Accessible without login Plain text file semantic.php Example Example script
  Accessible without login Plain text file spectre.php Example Example script
  Accessible without login Plain text file tailwind.php Example Example script
  Accessible without login Plain text file uikit.php Example Example script
  Accessible without login Plain text file _init.php Example Example script
  Accessible without login Plain text file _menu.php Example Demo menu
  Accessible without login Plain text file _ribbon.php Aux. Auxiliary script

  Files folder image Files  /  issues  
File Role Description
Files folder image13 (1 file)
Files folder image14 (2 files)

  Files folder image Files  /  issues  /  13  
File Role Description
  Plain text file index.php Class Class source

  Files folder image Files  /  issues  /  14  
File Role Description
  Accessible without login Plain text file create_msg.php Example Example script
  Accessible without login Plain text file msg.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageCore (4 files)
Files folder imageExceptions (3 files)
Files folder imageTemplates (10 files)
  Plain text file BaseTemplate.php Class base template
  Plain text file Flash.php Class flash wrapper
  Accessible without login Plain text file function.php Aux. function
  Plain text file TemplateFactory.php Class template factory
  Plain text file TemplateInterface.php Class template interface
  Plain text file Templates.php Class templates

  Files folder image Files  /  src  /  Core  
File Role Description
  Plain text file Engine.php Class Class source
  Plain text file MessageManager.php Class Class source
  Plain text file SessionManager.php Class Class source
  Plain text file TemplateManager.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file FlashSingletonException.php Class exception class
  Plain text file FlashTemplateException.php Class exception class
  Plain text file FlashTemplateNotFoundException.php Class exception class

  Files folder image Files  /  src  /  Templates  
File Role Description
  Plain text file BootstrapTemplate.php Class Class source
  Plain text file BulmaTemplate.php Class Class source
  Plain text file FoundationTemplate.php Class Class source
  Plain text file HalfmoonTemplate.php Class Class source
  Plain text file MaterializeTemplate.php Class Class source
  Plain text file PrimerTemplate.php Class Class source
  Plain text file SemanticTemplate.php Class Class source
  Plain text file SpectreTemplate.php Class Class source
  Plain text file TailwindTemplate.php Class Class source
  Plain text file UikitTemplate.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file BadTemplate.php Class test template
  Plain text file DisplayTest.php Class Class source
  Accessible without login Plain text file FactoryTest.php Test test factory
  Accessible without login Plain text file FlashTest.php Test general test

 Version Control Unique User Downloads Download Rankings  
 98%
Total:240
This week:4
All time:7,997
This week:117Up