toastr.js, pnotify.js 为 Laravel 清理通知

v1.3 2023-10-05 03:01 UTC

This package is auto-updated.

Last update: 2024-09-05 04:55:43 UTC


README

👀 此软件包帮助您将通知添加到您的 Laravel 项目中。

notify

安装

您可以使用 composer 安装此软件包

$ composer require pixelcostudios/notify

用法

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

  1. 添加您的样式链接标签或 @notify_css
  2. 添加您的脚本链接标签或 @notify_js
  3. 添加 @notify_render 来渲染您的通知
  4. 在控制器中使用 notify() 辅助函数来设置 info、success、warning 或 error 的 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');

Laravel 版本 < 5.5 的服务提供者

在 Laravel 5.5 及更高版本中,如果启用了自动发现,则可以跳过此步骤

将服务提供者添加到 config/app.php 中...

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

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

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

配置

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