gally90/laravel-notify

Laravel 通知

dev-master 2019-10-12 16:15 UTC

This package is auto-updated.

Last update: 2024-09-13 03:42:58 UTC


README

这是一个 Laravel 6 兼容的 Gloudemans\Notify 包的包装。

Laravel 通知

一些在网站上轻松获取便捷的弹出通知的有用工具。支持:Toastr、SweetAlert、Bootbox 和原生通知!

安装

通过 Composer 安装该包。使用以下命令通过 Composer 需求此包

composer require gally90/laravel-notify

接下来,您需要通过添加服务提供者在 Laravel 中注册该包。为此,打开您的 config/app.php 文件,并在 providers 数组中添加新行

Gloudemans\Notify\LaravelNotifyServiceProvider::class,

现在,该包已注册,您可以使用以下命令将包配置文件复制到您的本地配置中

artisan vendor:publish --provider="Gloudemans\Notify\LaravelNotifyServiceProvider" --tag="config"

现在您已准备好在 Laravel 应用程序中使用通知包了。

可选

如果您想使用 Notification 门面,请在 aliases 数组中添加新行

'Notification' => Gloudemans\Notify\Notifications\NotificationFacade::class,

使用方法

实际上使用此包非常简单。向应用程序添加通知实际上只需要两步。

添加通知

首先,当然,您需要一种方法将通知闪存到会话中,以便在下一个请求中可用。如果您已将 Gloudemans\Notify\Notifications\Notification 类注入到,例如,您的控制器中,将通知闪存到会话中就像这样简单

$this->notification->add('success', 'Notification message', 'Notification title');

如果您使用的是门面,那也和上面一样简单

Notification::add('success', 'Notification message', 'Notification title');
  • 第一个参数是通知的类型,该包理解四种类型的通知: successinfowarningerror
  • 第二个参数是通知的消息。
  • 第三个 (可选) 参数是通知的标题。

为了让生活更加简单,还有四个不同类型的辅助方法。因此,您不必手动提供通知类型,只需调用一个以类型为名称的方法即可

    $this->notification->success('Success notification');
    $this->notification->info('Info notification');
    $this->notification->warning('Warning notification');
    $this->notification->error('Error notification');

最后,您还可以将 Gloudemans\Notify\Notifications\AddsNotifications 特性添加到您的类中,这将为添加通知提供方法

    use AddsNotifications;
    
    public function yourMethod()
    {
        $this->notify()->success(...); // Also info(), warning(), error()
        $this->notifySuccess(...);
        $this->notifyInfo(...);
        $this->notifyWarning(...);
        $this->notifyError(...);
    }

显示通知

第二步是在您的网站上实际显示通知。所有通知都可以使用 Gloudemans\Notify\Notifications\Notification 上的 render() 方法渲染。

因此,一个可能性是在您的其中一个视图中添加以下内容

<?php app('Gloudemans\Notify\Notifications\Notification')->render(); ?>

但是,如果您和我一样不喜欢在视图中看到这样的代码,有一个很棒的 Blade 指令可用

@notifications

将此 Blade 指令添加到您的其中一个视图中将为该包提供渲染通知的 JavaScript 输出的位置

推荐在您的 'master' 布局文件底部渲染通知

渲染器

该包使用专用的 'renderer' 类来渲染通知。默认情况下,您可以从中选择:native(默认)、toastrsweetalertbootbox。要更改包使用的渲染器,只需更新 notifications.php 配置文件中 notifications 的值。

原生渲染器是唯一不需要任何额外 JavaScript 库的渲染器,因为它使用简单的 alert() 函数来显示通知。

对于所有其他渲染器,必要的脚本和样式表都已包含在包中,可以通过以下命令将其复制到您的公共目录:

artisan vendor:publish --provider="Gloudemans\Notify\LaravelNotifyServiceProvider" --tag="assets"

当然,您可以手动下载它们或使用其他服务获取,并将它们包含在您的资产构建序列中。

确保在调用 render() 方法之前添加脚本

扩展

如果您想在项目中使用其他库进行通知,这是完全可以的!您需要做的是创建自己的渲染器并更改Laravel的服务容器中的一个绑定。

您的自定义渲染器必须实现 Gloudemans\Notify\Notifications\NotificationRenderer 接口,这强制您实现一个简单的方法

    /**
     * Render the notifications as HTML/JavaScript
     *
     * @param  array $notifications
     * @return string
     */
    public function render(array $notifications);

创建自定义渲染器后,您可以像这样将其绑定到接口:

    $this->app->bind(
        'Gloudemans\Notify\Notifications\NotificationRenderer',
        'App\Renderers\MyCustomRenderer'
    );

这就是您需要做的,以使用自定义渲染器扩展该包。