百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

alerts

> 编程语言
开源

Alerts 是一个处理全局站点消息的包。

348 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Alerts 是一个处理全局站点消息的包。

Alerts for Laravel (v5 to v12)

Global site messages in Laravel. Helps trigger notification bubbles with a simple API, both in the current page, and in the next page (using flash data).

Table of Contents

  • Installation
  • Configuration
  • Usage
    • Adding Alerts
    • Adding Alerts Through Alert Levels
    • Flashing Alerts To The Session
    • Displaying Alerts
  • Changelog
  • License

Installation

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

bash
$ composer require prologue/alerts

For Laravel 5.4 and below, register the service provider in app/config/app.php.

php
'Prologue\Alerts\AlertsServiceProvider',

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

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:

bash
$ 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.

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

php
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

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

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

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

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

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

Display all messages for each alert level:

php
@foreach (Alert::getMessages() as $type => $messages)
    @foreach ($messages as $message)
        

    @endforeach
@endforeach

Checking For Alerts

Sometimes it can be important to see if alert's do exist, such as only load javascript/styles if they are there are some (helps for better performance).

You can check if there are any errors.

php
Alert::has(); // Will check for any alerts
Alert::has('error'); // Will check for any alerts listed as errors.

You can also get the current number of alerts.

php
Alert::count(); // Will give you a total count of all alerts based on all levels within your alerts config.
Alert::count('error'); // Will tell you only the amount of errors and exclude any levels.

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

Credits

Created by Dries Vints - he first got the idea after a blog post 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 thanks to its use in Backpack for Laravel.

Changelog

You view the changelog for this package here.

License

Prologue Alerts is licensed under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言