kallbuloso/notify

toastr.js, pnotify.js 为 Laravel 5, 6, 7 和 Lumen 清除通知

v1.0.0 2020-04-28 04:04 UTC

This package is auto-updated.

Last update: 2024-09-28 13:52:24 UTC


README

👀 该包帮助您在 Laravel 5 和 Lumen 项目中添加通知。

Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

notify

安装

您可以使用 composer 安装此包。

$ composer require kallbuloso/notify

然后将服务提供者添加到 config/app.php。在 Laravel 5.5 及更高版本中,如果启用了包自动发现,则可以跳过此步骤。

'providers' => [
    ...
    kallbuloso\Notify\NotifyServiceProvider::class
    ...
];

作为可选步骤,如果您想修改默认配置,可以发布配置文件

$ php artisan vendor:publish --provider='kallbuloso\Notify\NotifyServiceProvider' --tag="config"

对于 Lumen

  1. bootstrap/app.php
    • 取消注释 $app->withFacades();
    • 添加 ToastrServiceProvider 的绑定:$app->register(kallbuloso\Notify\NotifyServiceProvider::class);
  2. 添加 config/session.php,因为 Lumen 默认不包含它。您可以从 Laravel 官方仓库 中获取 session.php

用法

将 jQuery 和您的通知插件资源包含在视图模板中

  1. 添加您的样式链接标签或 @notify_css
  2. 添加您的脚本链接标签或 @notify_js
  3. @notify_render 添加到渲染通知
  4. 在控制器中使用 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>kallbuloso/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' => \kallbuloso\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' => \kallbuloso\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' => \kallbuloso\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