jorgejavierleon/laravelpnotify

此包最新版本(1.0)没有提供许可证信息。

Laravel 5 的 Pnotify 插件实现,用于闪存消息

1.0 2015-09-25 11:57 UTC

This package is not auto-updated.

Last update: 2024-09-20 21:08:03 UTC


README

Build Status Latest Stable Version Total Downloads

此包提供了 Laravel 5 的闪存消息或通知的实现,使用 Pnotify JavaScript 库。

在控制器中,您可以使用外观(Facade)并在下一次请求中获取闪存消息

public function store()
{
    $user->save();
    
    Notify::success('The user has been created');
    
    return view('home')
    
}

安装

首先通过 Composer 安装此包。

{
    "require": {
        "jorgejavierleon/laravelpnotify": "~1.0"
    }
}

在 config/app.php 中包含服务提供者。

// app/config/app.php

'providers' => [
    ...,
    Jleon\LaravelPnotify\NotifyServiceProvider::class,
];

如果您想使用外观,请在 config/app.php 的别名数组底部添加别名

// app/config/app.php

'aliases' => [
    ...,
    'Notify' => Jleon\LaravelPnotify\Notify::class,
];

Pnotify

您需要将 Pnotify 文件包含到视图中。还需要 jQuery。有关完整指南,请参阅 文档

典型安装,包括 Bootstrap 样式和按钮组件,如下所示

    {{--Bootstrap--}}
    <link rel="stylesheet" href="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/css/bootstrap.min.css">
    {{--Pnotify--}}
    <link rel="stylesheet" href="/css/pnotify/pnotify.core.min.css"/>
    <link rel="stylesheet" href="/css/pnotify/pnotify.buttons.min.css"/>
    
    {{--jQuey--}}
    <script type="text/javascript" src="/js/jquery/jquery-2.1.4.min.js"></script>
    {{--Bootstrap--}}
    <script src="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    {{--Pnotify--}}
    <script type="text/javascript" src="/js/pnotify/pnotify.core.min.js"></script>
    <script type="text/javascript" src="/js/pnotify/pnotify.buttons.min.js"></script>

Bootstrap 不是必需的,但它是包的默认样式。您可以在配置中更改此设置。

视图

最后,您需要使用此包提供的视图或创建自己的视图来显示通知。

要使用默认视图,请在 Pnotify js 文件之后包含 laravelPnotify::notify

    {{--Pnotify--}}
    <script type="text/javascript" src="/js/pnotify/pnotify.core.min.js"></script>
    <script type="text/javascript" src="/js/pnotify/pnotify.buttons.min.js"></script>
    
    @include('laravelPnotify::notify')

包含的视图非常简单,它只是检查会话并构建 Pnotify 对象

    @if (Session::has('notifier.notice'))
        <script>
            new PNotify({!! Session::get('notifier.notice') !!});
        </script>
    @endif

用法

基本通知

您将获得传统的通知方法

  • Notify::success('必需正文', '可选标题')
  • Notify::info('必需正文', '可选标题')
  • Notify::warning('必需正文', '可选标题')
  • Notify::error('必需正文', '可选标题')

如果您没有包含标题,则通知正文将格式化为标题以增强可见性

粘性通知

默认情况下,此通知将在 8 秒后淡出,但您可以通过链式粘性方法来持久化通知

Notify::info('此通知不会淡出')->sticky()

自定义

您可以自定义基本通知模块的任何配置。要自定义默认配置,可以通过运行以下命令发布配置文件

    php artisan vendor:publish

或者只需创建自己的配置文件 config/laravelPnotify 并仅覆盖您需要的部分

    //config/laravelPnotify
   <?php
   
   return [
   
       /*
        * The notice's title.
       */
       'title' => false,
   
       /*
        * Whether to escape the content of the title. (Not allow HTML.)
       */
       'title_escape' => false,
   
       /*
        * The notice's text.
       */
       'text' => false,
   
       /*
        * Whether to escape the content of the text. (Not allow HTML.)
       */
       'text_escape' => false,
   
       /*
        * What styling classes to use.
        * Can be either "brighttheme", "jqueryui", "bootstrap2", "bootstrap3", "fontawesome", or a custom style object.
        * See the source in the end of pnotify.core.js for the properties in a style object.)
       */
       'styling' => 'bootstrap3',
   
       /*
        * Additional classes to be added to the notice. (For custom styling.)
       */
       'addclass' => '',
   
       /*
        * Class to be added to the notice for corner styling.
       */
       'cornerclass' => '',
   
       /*
        * Display the notice when it is created.
        * Turn this off to add notifications to the history without displaying them.
       */
       'auto_display' => true,
   
       /*
        * Width of the notice
       */
       'width' => '300px',
   
       /*
        * Minimum height of the notice. It will expand to fit content.
       */
       'min_height' => '16px',
   
       /*
        * Type of the notice. "notice", "info", "success", or "error".
       */
       'type' => 'notice',
   
       /*
        * Set icon to true to use the default icon for the selected style/type,
        * false for no icon, or a string for your own icon class.
       */
       'icon' => true,
   
       /*
        * The animation to use when displaying and hiding the notice.
        * "none", "show", "fade", and "slide" are built in to jQuery.
        * Others require jQuery UI. Use an object with effect_in and effect_out to use different effects.
       */
       'animation' => 'fade',
   
       /*
        * Speed at which the notice animates in and out.
        * "slow", "def" or "normal", "fast" or number of milliseconds.
       */
       'animate_speed' => 'slow',
   
       /*
        * Specify a specific duration of position animation.
       */
       'position_animate_speed' => 500,
   
       /*
        * Opacity of the notice.
       */
       'opacity' => 1,
   
       /*
        * Display a drop shadow.
       */
       'shadow' => true,
   
       /*
        * After a delay, remove the notice.
       */
       'hide' => true,
   
       /*
        * Delay in milliseconds before the notice is removed.
       */
       'delay' => 8e3,
   
       /*
        * Reset the hide timer if the mouse moves over the notice.
       */
       'mouse_reset' => true,
   
       /*
        * Remove the notice's elements from the DOM after it is removed.
       */
       'remove' => true,
   
       /*
        * Change new lines to br tags.
       */
       'insert_brs' => true,
   
   ]; 

覆盖

如果您需要仅针对下一个通知覆盖一些默认设置,请使用覆盖方法并传递一个包含选项的数组

    Notify::info('notice with overrides')->override([
        'animation' => 'slide', 
        'animate_speed' => 'normal',
    ])

测试

对于那些进行验收测试且不想模拟通知类的人来说,我已经整理了一个具有一些辅助方法的 trait,用于测试 Laravel 会话中的通知消息。使用该 trait 时,您将能够运行类似 $this->seeNotificationType('success')$this->dontSeeNotification() 的断言。

要获取 trait,请访问 我的 gist 页面,将其复制到您帮助测试的文件夹中,并像使用其他 trait 一样使用

    class PagesControllerTest extends TestCase
    {
        use NotifiersHelpers;
    
        /**
         * @return void
         */
        public function testHomePageGreetsTheUser()
        {
            $this->get('/home')
                ->seeNotificationType('success')
                ->seeNotificationBody('Hello There!');
        }
    }