gloudemans/notify

Laravel 通知

1.0.0 2015-08-22 14:26 UTC

This package is auto-updated.

Last update: 2024-09-06 09:34:33 UTC


README

Build Status Total Downloads License

一些在您的网站上获取便捷的闪存通知的有用工具!开箱即用的支持:Toastr、SweetAlert、Bootbox 和原生通知!

安装

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

composer require gloudemans/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'
    );

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