Download the PHP package hampel/alerts without Composer
On this page you can find all versions of the php package hampel/alerts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hampel/alerts
More information about hampel/alerts
Files in hampel/alerts
Package alerts
Short Description Package for managing Laravel session messages and alerts in view responses
License MIT
Homepage https://bitbucket.org/hampel/alerts
Informations about the package alerts
Alerts for Laravel
This package provides session-driven alerts for Laravel and provides alert templates for both Bootstrap and Foundation based front-end frameworks.
By Simon Hampel
Installation
To install through composer, run the following command:
composer require hampel/alerts
If you want to change the default Alert configuration or views, first publish them using the command:
The config file can then be found in config/alerts.php
, and the views in resources/views/vendor/alerts
Usage
The alerts functionality is based around two classes - the first is an AlertManager class, which you can access in your code via the Alert facade (or via the container $app['alerts']). The AlertManager class uses a Laravel MessageBag to store alert a collection of alerts, grouped by alert key (warning, error, success, etc).
Once you have finished adding alerts, you can then "flash" them to the session so that the alert messages are then available after a page redirect.
At this point, the second part of the functionality comes into play. The package provides a view composer, which waits for a particular view to be rendered and then executes, injecting the stored alert information which is retrieved from the session. The alerts are essentially rendered as a series of sub-views which are inserted into the base view using a configurable variable.
We can easily add alerts to the AlertManager using the built-in functionality:
There are four alert levels provided by default: info
, warning
, error
, success
. Note that any alert level passed
to the add function which is not in the configured list of alert levels, will be ignored when rendering the view.
You can add a message to the AlertManager using a shorthand function corresponding to the configured alert levels:
If you prefer to use the language translation files for your messages, you can simply specify a translation key and the AlertManager will substitute the corresponding language entry and optionally apply any replacements too. Note that this only works if you use the shorthand functions to add your messages.
Alternatively, you could apply the translation yourself:
You can retrieve the underlying MessageBag implementation and merge an existing MessageBag.
To pass the alert messages to a view response, you will need to flash the data to the session.
You can do this in several ways - the AlertManager class provides a flash
function, which sets the session key for
you automatically, or you could do it as part of a redirect response. If not using the built-in flash
function, make
sure you specify the name of the session key used in the config file 'alerts::session_key'
, or use the helper function
Alerts::getSessionKey()
:
The package provides a view composer which automatically renders alerts whenever the view is called. There are several ways to do this.
By default, there is a Blade template alerts.blade.php
which you can include in your base view. The view composer is
configured by default to watch this view and render the alerts whenever this view is rendered. To use, simply @include
the template in your view:
If you prefer to do it yourself, you can change the configuration of the view composer to watch a different view,
perhaps even your layouts.base
view. The composer will generate the alert data and bind it to the configured variable
name, which you can then simply echo out in your view.
There are templates provided for both Bootstrap and Foundation, although Foundation is the default - you'll need to change the configuration setting if you use Bootstrap. Alternatively, you can provide your own view template to use to render each alert message. Refer to the configuration file for more information on what the settings do.
All versions of alerts with dependencies
illuminate/support Version 5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/session Version 5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/translation Version 5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0