Code Monkey home page Code Monkey logo

alerts's Introduction

Alerts for Laravel 5

Build Status Quality Score Software License Packagist Version Total Downloads

Alerts is a package that handles global site messages in Laravel 5.

I first got the idea for creating this package after a blog post I read by Todd Francis. This package uses much of the concepts of his blog post as well as the concept of alert levels which Illuminate's Log package uses.

Maintained by Cristian Tabacitu

Table of Contents

Installation

You can install the package for your Laravel 5 project through Composer.

$ composer require prologue/alerts

Register the service provider in app/config/app.php.

'Prologue\Alerts\AlertsServiceProvider',

Add the alias to the list of aliases in app/config/app.php.

'Alert' => 'Prologue\Alerts\Facades\Alert',

Configuration

The packages provides you with some configuration options.

To create the configuration file run this command in your command line app:

$ php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"

The configuration file will be published here: config/prologue/alerts.php.

Usage

Adding Alerts

Since the main AlertsMessageBag class which powers the package is an extension of Illuminate's MessageBag class we can leverage its functionality to easily add messages.

Alert::add('error', 'Error message');

Adding Alerts Through Alert Levels

By default, the package has some alert levels defined in its configuration file. The default levels are success, error, warning and info. The AlertsMessageBag checks if you call one of these levels as a function and registers your alert which you provided with the correct key.

This makes adding alerts for certain alert types very easy:

Alert::info('This is an info message.');
Alert::error('Whoops, something has gone wrong.');

You can of course add your own alert levels by adding them to your own config file. See above on how to publish the config file.

Flashing Alerts To The Session

At some times you want to remember alerts when you're, for example, redirecting to another route. This can be done by calling the flash method. The AlertsMessageBag class will put the current set alerts into the current session which can then be used after the redirect.

// Add some alerts and flash them to the session.
Alert::success('You have successfully logged in')->flash();

// Redirect to the admin dashboard.
return Redirect::to('dashboard');

// Display the alerts in the admin dashboard view.
return View::make('dashboard')->with('alerts', Alert::all());

Displaying Alerts

Remember that the AlertsMessageBag class is just an extension of Illuminate's MessageBag class, which means we can use all of its functionality to display messages.

@foreach (Alert::all() as $alert)
    {{ $alert }}
@endforeach

Or if you'd like to display a single alert for a certain alert level.

@if (Alert::has('success'))
    {{ Alert::first('success') }}
@endif

Display all messages for each alert level:

@foreach (Alert::getMessages() as $type => $messages)
    @foreach ($messages as $message)
        <div class="alert alert-{{ $type }}">{{ $message }}</div>
    @endforeach
@endforeach

If you'd like to learn more ways on how you can display messages, please take a closer look to Illuminate's MessageBag class.

Changelog

You view the changelog for this package here.

License

Prologue Alerts is licensed under the MIT License.

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.