yoeunes / notify
此包已被弃用且不再维护。作者建议使用 php-flasher/flasher-laravel 包。
toastr.js, pnotify.js 为 Laravel 5, 6, 7, 8 和 Lumen 提供通知功能
v1.0.7
2021-02-07 22:09 UTC
Requires
- php: >=7.0
- illuminate/session: ^5.5 || ^6.0 || ^7.0 || ^8.0
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- orchestra/testbench: ^2.0.4 || ^3.0 || ^4.0 || ^5.0
- phpunit/phpunit: ^4.8.36 || ^5.7 || ^6.0 || ^7.0 || ^8.3 || ^9.0
README
我正在为 Laravel 和 Symfony 开发一个更高级、更灵活的解决方案,包括更多驱动程序,例如:Tailwindcss、bootstrap、Noty、Sweet ALert、Notyf 和 Pnotify
我目前正在记录所有部分,但如果你有时间可以在这里查看: https://php-flasher.github.io/
👀 此包可以帮助您向 Laravel 5 和 Lumen 项目添加通知。
安装
您可以使用 composer 安装此包
$ composer require yoeunes/notify
然后,将服务提供者添加到 config/app.php
。在 Laravel 5.5 及以上版本中,如果启用了包自动发现,此步骤可以跳过。
'providers' => [ ... Yoeunes\Notify\NotifyServiceProvider::class ... ];
作为可选步骤,如果您想修改默认配置,可以发布配置文件
$ php artisan vendor:publish --provider='Yoeunes\Notify\NotifyServiceProvider' --tag="config"
Lumen 使用方法
- 在
bootstrap/app.php
- 取消注释
$app->withFacades();
- 添加对 ToastrServiceProvider 的绑定:
$app->register(Yoeunes\Notify\NotifyServiceProvider::class);
- 取消注释
- 添加
config/session.php
,因为默认情况下 Lumen 中不存在。您可以从 Laravel 官方仓库 获取session.php
使用方法
在您的视图模板中包含 jQuery 和您的通知插件资源
- 添加您的样式链接标签或
@notify_css
- 添加您的脚本链接标签或
@notify_js
- 添加
@notify_render
以渲染您的通知 - 在控制器中使用
notify()
辅助函数设置 toast 通知(信息、成功、警告或错误)
// 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' => \Yoeunes\Notify\Notifiers\Toastr::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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' => \Yoeunes\Notify\Notifiers\Pnotify::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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' => \Yoeunes\Notify\Notifiers\SweetAlert2::class, 'notify_js' => [ 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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' => [], ], ];
致谢
许可
MIT