jubayed / laravel-notify
toastr.js, pnotify.js 为 Laravel 清理通知
dev-master
2021-02-09 15:10 UTC
Requires
- php: ^7.1|^8.0
- illuminate/session: ^8.5
- illuminate/support: ^8.5
This package is auto-updated.
Last update: 2024-09-10 00:34:32 UTC
README
安装
您可以使用 composer 安装此包
$ composer require jubayed/laravel-notify
然后,将服务提供者添加到 config/app.php
。在 Laravel 5.5 及更高版本中,如果启用了包自动发现,则可以跳过此步骤。
'providers' => [ ... Jubayed\Notify\NotifyServiceProvider::class ... ];
如果需要修改默认配置,可以发布配置文件作为可选操作
$ php artisan vendor:publish --provider='Jubayed\Notify\NotifyServiceProvider' --tag="config"
使用方法
将 jQuery 和您的通知插件资源包含在视图模板中
- 添加您的样式链接标签或
@notify_css
- 添加您的脚本链接标签或
@notify_js
- 添加
@notify_render
以渲染您的通知 - 在控制器内部使用
notify()
辅助函数设置信息、成功、警告或错误的通知
// Display an info toast with no title notify()->info('Are you the 6 fingered man?')
例如
<?php namespace App\Http\Controllers; use App\Post; use App\Http\Requests\PostRequest; use Illuminate\Database\Eloquent\Model; class PostController extends Controller { public function store(PostRequest $request) { $post = Post::create($request->only(['title', 'body'])); if ($post instanceof Model) { notify()->success('Data has been saved successfully!'); return redirect()->route('posts.index'); } notify()->error('An error has occurred please try again later.'); return back(); } }
之后,在视图底部添加 notify_render()
以实际渲染通知。
<!doctype html> <html> <head> <title>yoeunes/toastr</title> @notify_css </head> <body> </body> @notify_js @notify_render </html>
其他选项
// Set a warning toast, with no title notify()->warning('My name is Inigo Montoya. You killed my father, prepare to die!') // Set a success toast, with a title notify()->success('Have fun storming the castle!', 'Miracle Max Says') // Set an error toast, with a title notify()->error('I do not think that word means what you think it means.', 'Inconceivable!') // Override global config options from 'config/notify.php' notify()->success('We do have the Kapua suite available.', 'Turtle Bay Resort', ['timeOut' => 5000]) // for pnotify driver notify()->alert('We do have the Kapua suite available.', 'Turtle Bay Resort', ['timeOut' => 5000])
其他 API 方法
// 您也可以使用方法链将多个消息连接在一起
notify()->info('Are you the 6 fingered man?')->success('Have fun storming the castle!')->warning('doritos');
配置
// config/notify.php <?php return [ 'default' => 'toastr', 'toastr' => [ 'class' => \Jubayed\Notify\Notifiers\Toastr::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js', ], 'notify_css' => [ 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css', ], 'types' => [ 'error', 'info', 'success', 'warning', ], 'options' => [], ], 'pnotify' => [ 'class' => \Jubayed\Notify\Notifiers\Pnotify::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.js', ], 'notify_css' => [ 'https://cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.css', 'https://cdnjs.cloudflare.com/ajax/libs/pnotify/3.2.1/pnotify.brighttheme.css', ], 'types' => [ 'alert', 'error', 'info', 'notice', 'success', ], 'options' => [], ], 'sweetalert2' => [ 'class' => \Jubayed\Notify\Notifiers\SweetAlert2::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.28.1/sweetalert2.min.js', 'https://cdn.jsdelivr.net.cn/npm/promise-polyfill', ], 'notify_css' => [ 'https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.28.1/sweetalert2.min.css', ], 'types' => [ 'error', 'info', 'question', 'success', 'warning', ], 'options' => [], ], ];
弹窗框(Laravel)
一个辅助包,通过外观或辅助函数将 Bootstrap 弹窗快速显示到浏览器中。
<div class="alert alert-info fade in"> <i class="fa-fw fa fa-smile-o"></i> <strong>Title</strong> Description </div>
使用方法
在任何视图文件中。
@include('alert::alert')
在任何控制器中。
public function index() { // helper function - default to the 'info' alert('Title', 'Lorem Ipsum'); // return object first alert()->info('Title', 'Lorem Ipsum'); // via the facade Alert::info('Title', 'Lorem Ipsum'); return view('home'); }
不同的 '级别' 有
alert()->info('标题', 'Lorem Ipsum');
alert()->success('标题', 'Lorem Ipsum');
alert()->warning('标题', 'Lorem Ipsum');
alert()->danger('标题', 'Lorem Ipsum');
不同的参数
alert()->info('标题', 'Lorem Ipsum', false);
// 不显示图标alert()->info('标题', 'Lorem Ipsum', 'smile-o');
// 指定图标类alert()->message('标题', 'Lorem Ipsum', 'smile-o', 'info');
// 指定级别的类型alert()->message('标题', 'Lorem Ipsum', 'smile-o', 'info', false);
// 不显示 '关闭' 按钮
视图部分可在此处找到 resources\views\vendor\alert\alert.blade
。