hieu-le / laravel-alert
Laravel 全局站点消息系统
2.2.0
2017-08-24 08:32 UTC
Requires
- php: >=5.6.0
- illuminate/config: ^5.0
- illuminate/session: ^5.0
- illuminate/support: ^5.0
- illuminate/view: ^5.0
Requires (Dev)
- orchestra/testbench: ^3.0
- phpunit/phpunit: ~4.0
README
Laravel 4 的全局站点消息系统(对于 Laravel 5,请参阅 laravel-5 分支)
本包的目的
- 以多种语义意义生成警报:成功、信息、警告、错误
- 将收集的消息传递到下一个请求
- 在可定制的视图中显示收集的消息,默认为 Bootstrap 3 警报
- 支持每种消息类型的图标。
安装
要使用此包,您需要 Composer。对于不熟悉这个强大工具的人来说,这里有一个很好的使用 Composer 的教程。
将此添加到您的 composer.json
文件中。
"hieu-le/laravel-alert": "~1.0"
运行 composer update
以更新所有依赖项。之后,通过在 app/config/app.php
文件的 providers
数组中添加此行来注册 Laravel 的服务提供程序:
'HieuLe\Alert\AlertServiceProvider',
并在 app/config/app.php
文件的 aliases
数组中添加此别名:
'Alert' => 'HieuLe\Alert\Facades\Alert',
使用方法
要添加新消息,请使用以下四种方法之一。每种方法的名称都是您要添加的消息类型。
Alert::success($message); # or Alert::info($message); # or Alert::warning($message); # or Alert::error($message);
要使这些消息在下一个请求中可用,您必须至少调用一次 Alert::flash()
。
Alert::flash()
在您的视图中,使用 Alert::dump
方法来显示这些消息。默认情况下,每种类型的消息都以 Bootstrap 3 警报 格式渲染。您可以通过阅读下面的配置部分来更改它们的外观。
配置
您可以通过修改这些配置来获得对包的更多控制。首先,发布包配置,这样您就可以编辑复制的版本。
php artisan config:publish hieu-le/laravel-alert
打开 app/config/packages/hieu-le/laravel-alert/config
文件并更改这些设置:
session_key
:存储请求间消息的会话键的名称。您通常不需要修改其值。icon
:在默认视图中渲染每种类型消息时使用的图标。您可以通过删除某个键的值来禁用该类型消息的图标。view
:用于渲染每种类型消息的视图名称。在视图中,您可以使用 2 个变量:$icon
是此消息类型的图标;$messages
:当前消息类型的消息数组。
与 Laravel 验证错误结果一起工作
要显示验证错误,只需在 Alert::dump
方法中添加另一个参数。例如
// In each view, there is a special variable call $errors // This is the validation errors that is set by calling "withError" method as // guided at https://laravel.net.cn/docs/4.2/validation#error-messages-and-views echo Alert::dump($errors->all());