jambo8989/flash

此包将向您的应用程序发送闪光通知

v1.0.1 2022-02-21 12:22 UTC

This package is auto-updated.

Last update: 2024-09-21 18:21:57 UTC


README

Issues Latest Version License Stars Total Downloads

Laravel Flash Notification

闪光通知类型

Laravel Flash Notification

安装

使用composer快速安装。

运行

 composer require jambasangsang/flash

然后您可以将服务提供者添加到 config/app.php

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

'providers' => [
    Jambasangsang\Flash\FlashNotificationServiceProvider::class,
];

发布配置文件

php artisan vendor:publish --provider='Jambasangsang\Flash\FlashNotificationServiceProvider' --tag="flash-config"

如果未找到,请运行以下命令

 php artisan vendor:publish 

并选择 Jambasangsang\Flash\FlashNotificationServiceProvider

使用方法

1. 如果您的应用程序使用jQuery,请不要包含 [@jQuery],否则将以下代码添加到主视图模板中

CSS @flashStyle

JS @jQuery@flashScript@flashRender

示例

<!-- layouts/app.blade.php -->

<!doctype html>
<html>
    <head>
        
        @flashStyle
    </head>

    <body>
        
    @jQuery
    @flashScript
    @flashRender

    </body>
</html>

2. 在您的控制器中,在执行重定向之前...

示例

<?php

namespace App\Http\Controllers;

use App\Http\Requests\LevelStoreRequest;
use App\Models\Level;
use Illuminate\Http\RedirectResponse;
use App\Jambasangsang\Services\Levels\LevelService;
use Jambasangsang\Flash\Facades\LaravelFlash;

class LevelController extends Controller
{

    public function store(LevelStoreRequest $request, LevelService $levelService): RedirectResponse
    {
       
       try{
            $levelService->storeLevelData(new Level(), $request);

            LaravelFlash::withSuccess("Level added successfully!");

       }catch{

            LaravelFlash::withError("Woops!! an error check your input and try again!");
       }
        
        return redirect()->route('levels.index');
    }
}

您也可以使用以下其他选项

  • LaravelFlash::withInfo('本周你已支付账单!')
  • LaravelFlash::withSuccess('您的记录已成功保存!')
  • LaravelFlash::withWarning('您有一个安全问题,尝试修复它!')
  • LaravelFlash::withError('您的记录未保存失败!')

配置

以自定义您的闪光通知。
// config/flash.php
<?php

return [

    'options' => [
        'message'       => 'Default Message Here', //String
        'messageTextColor'   => '#ffff', //String
        'position'        => 'top-right', //String
        'customClass'     => '', //String
        'width'       => 'auto', //String Ex. 190px etc.
        'showCloseButton'         => true, //Boolean
        'closeButtonText'       => 'Close', //String
        'alertScreenReader'      => true, //Boolean
        'duration'       => 5000,
        'onClose'        => 'el', // Write your custom function here
        'closeButtonTextColor'      => '#FFFF',
    ],
];

致谢

许可

MIT