Giter VIP home page Giter VIP logo

flash-messages's Introduction

Flash Messages for Your Laravel App

This package is inspired by laracast/flash package.
Unlike that package, this package allows to use multiple flash messages during one request and set additional properties to HTML tag.

Installation

First, pull in the package through Composer.

Run composer require valkovic/flash

And then, if using Laravel 5, include the service provider within config/app.php.

'providers' => [
    \Valkovic\Flash\FlashServiceProvider::class,
],

For correct use, you must also register middleware, that cares about loading and saving flash messages during requests. Attach middleware to any group you want, for example I will attach it to web group. Settings are at file app/Http/Kernel.php.

'web' => [
    \Valkovic\Flash\FlashMiddleware::class,
],

You can also register Facade for Flash package. Just set alias in config/app.php

'aliases' => [
    'Flash' => Valkovic\Flash\FlashFacade::class,
];

Usage

Within your controllers, before you perform a redirect...

public function store()
{
    flash('Welcome Aboard!');

    return home();
}

You may also do:

  • flash('Message', 'none')
  • flash('Message', 'info')
  • flash('Message', 'primary')
  • flash('Message', 'success')
  • flash('Message', 'warning')
  • flash('Message', 'error')
  • flash()->overlay('Modal Message', 'Modal Title')
  • flash('Message')->important()

Behind the scenes, this will set valkovic.flash-messages key in the session:

During next request, Valkovic\Flash\FlashMiddleware will load messages from session and share them to all views as Flashes variable.
Then, in your view, you can iterate over them.

<div class="notifications">
    @foreach($flashes as $flash)
        <div>
            {{$flash->message}}
        </div>
    @endforeach
</div>

If you set type of message, that type is attach to message as CSS class. If you want to render also that class, you must call $flash->renderProperties(). Because this text contains HTML specific tag, you must not escape output from that method.

<div class="notifications">
    @foreach($flashes as $flash)
        <div {!! $flash->renderProperties() !!}>
            {{$flash->message}}
        </div>
    @endforeach
</div>

Note that type of messages reflect classes in Twitter Bootstrap.

If you with, you can add more classes or even properties to tag. For example, this code

flash('Some message','primary',['class'=>'myClass','id'=>'flashMessage']);

with previous blade example will produce following code.

<div class="notifications">
        <div class="primary myClass" id="flashMessage">
            Some message
        </div>
</div>

Another usage

This package provides Flash model, Flash facade and also flash helper function. You are free yo choose, which approach more suits for you. If you want to use Flash facade, you will first need to register alias for it, as is noted in Install section.

These commands will have same results.

flash('Message','success');
Flash()->success('Message');
flash()->success('Message'); //Note that flash() without parameters return Flash Model

// OR INSIDE CLASS
private $flash;
public function __construct(\Valkovic\Flash\Flash $flash) {
    $this->flash = $flash;
}
public function send(User $user) {
    //store User
    $this->flash->success('Message');
}

If you don't want to use Bootstrap specific classes, you are free to use array of properties as second parameter to message method or flash helper.

flash('Message',['class'=>'myClass','id'=>'flashMessage']);
Flash()->message('Message',['class'=>'myClass','id'=>'flashMessage']);

//with template before will produce
<div class="notifications">
        <div class="myClass" id="flashMessage">
            Some message
        </div>
</div>

Hiding Flash Messages

A common desire is to display a flash message for a few seconds, and then hide it. To handle this, write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing </body> tag. Because you can attach id property to tag, you can refer to id property directly.

flash('Message',['id'=>'toHide']);

<script>
$('#toHide').delay(3000).fadeOut(350);
</script>

Custom views

This package in current version don't provide views, to show flash messages.

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.