Git Product home page Git Product logo

laravel-flash's Introduction

A lightweight package to flash messages

Latest Version on Packagist Build Status StyleCI Quality Score Total Downloads

This is a lightweight package to send flash messages in Laravel apps. A flash message is a message that is carried over to the next request by storing it in the session. This package only supports one single flash message at a time.

This is how it can be used:

class MySpecialSnowflakeController
{
    public function store()
    {
        // …

        flash('My message', 'my-class');

        return back();
    }
}

In your view you can do this:

@if(flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>
@endif

Installation

You can install the package via composer:

composer require spatie/laravel-flash

Usage

Here is an example on how to flash a message.

class MyController
{
    public function store()
    {
        // …

        flash('My message');

        return back();
    }
}

In your view you can use it like this

@if(flash()->message)
    <div>
        {{ flash()->message }}
    </div>
@endif

Using a class name to style the displayed message

You can add a class as the second parameter. This is typically used to style the output in your HTML.

class MyController
{
    public function store()
    {
        // …

        flash('My message', 'my-class'));

        return back();
    }
}

In your view you can use the class like this:

@if(flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>
@endif

You can also set an array of classes. These will be output by flash()->class by imploding the array with a space-delimiter.

 flash('My message', ['my-class', 'another-class'])); // flash()->class output is: 'my-class another-class'

Adding your own methods

If you don't want to specify a class each time you flash a message you can add a method name to flash.

The easiest way is by passing an array to the levels method. The key is the method name that should be added to flash(). The value is the class that will automatically be used when rendering the message.

// this would probably go in a service provider

\Spatie\Flash\Flash::levels([
    'success' => 'alert-success',
    'warning' => 'alert-warning',
    'error' => 'alert-error',
]);

The above example will make these methods available on flash:

flash()->success('Hurray');
flash()->warning('Mayybeee');
flash()->error('Oh Oh');

Additionally, when you've added your own method, you can also pass that method name as a second parameter to flash itself:

flash('Hurray', 'success'); // `flash()->class` will output 'alert-success'

You can also add a method to flash by using macro.

Here's an example:

// this would probably go in a service provider

use Spatie\Flash\Message;

\Spatie\Flash\Flash::macro('warning', function(string $message) {
    return $this->flashMessage(new Message($message, 'alert alert-warning'));
});

You can now use a warning method on flash:

flash()->warning('Look above you!');

Alternatives

This package is intended to be lightweight. If you need things like multiple messages, support for Bootstrap, overlays, ... take a look at this excellent flash package by Jeffrey Way or Laraflash by Ilya Sakovich.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

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

laravel-flash's People

Contributors

brendt avatar davimug avatar drbyte avatar faustbrian avatar freekmurze avatar paulredmond avatar

Watchers

 avatar  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.