定制 haxneeraj/laravel-livewire4-toaster 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

haxneeraj/laravel-livewire4-toaster

Composer 安装命令:

composer require haxneeraj/laravel-livewire4-toaster

包简介

Elegant toast notifications for Laravel Livewire 4 + Alpine.js — event-driven, configurable, zero dependencies.

README 文档

README

Latest Version on Packagist License: MIT PHP Version

Elegant toast notifications for Laravel Livewire 4 + Alpine.js — event-driven, configurable, zero external dependencies.

Toast Preview Toast Preview Toast Preview Toast Preview

📸 Demo

Laravel Livewire4 Toaster Demo

✨ Features

  • Trait-based API$this->success('Saved!') in any Livewire component
  • Facade & HelperToast::error('Failed!') or toast('success', 'Done') from controllers
  • Event-driven — Livewire 4 dispatch() → Alpine x-on:toast.window
  • Auto-dismiss with animated progress bar & pause-on-hover
  • Queue mode — show one toast at a time with automatic queue
  • Duplicate handling — suppress or replace identical toasts
  • Configurable positions — top-right, top-left, bottom-right, bottom-left, top-center, bottom-center
  • Dark mode ready with Tailwind CSS
  • Accessiblerole="status", aria-live="polite"
  • Redirect support — flash toasts to session, inject on next page load
  • JavaScript APIwindow.Toast.success('Hello!') for frontend-only toasts
  • Publishable config & views — full control when you need it
  • Zero external dependencies — no toastr.js, no notyf, just Alpine.js

📦 Installation

1. Install via Composer

composer require haxneeraj/laravel-livewire4-toaster

The package auto-discovers its service provider and facade.

2. Publish Config (Optional)

php artisan vendor:publish --tag=toaster-config

This creates config/toaster.php where you can customize everything.

3. Add the Toast Hub to Your Layout

Place the toast-hub component in your main layout file, typically before </body>:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My App</title>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    @livewireStyles
</head>
<body>
    {{ $slot }}

    {{-- Toast notification container --}}
    <x-livewire-toaster::toast-hub />

    @livewireScripts
</body>
</html>

That's it! You're ready to toast. 🍞

🚀 Usage

In Livewire Components (Toastable Trait)

The Toastable trait is the primary API for Livewire components:

<?php

namespace App\Livewire;

use Haxneeraj\LivewireToaster\Traits\Toastable;
use Livewire\Component;

class ProfileForm extends Component
{
    use Toastable;

    public string $name = '';

    public function save()
    {
        // ... validate and save ...

        $this->success('Profile updated successfully!', 'Saved');
    }

    public function delete()
    {
        // ... delete logic ...

        $this->error('Profile has been deleted.', 'Deleted');
    }

    public function archive()
    {
        $this->warning('Profile archived. You can restore it within 30 days.', 'Archived', [
            'duration' => 5000,
        ]);
    }

    public function notify()
    {
        $this->info('You have 3 unread messages.');
    }

    public function customToast()
    {
        $this->toast('success', 'Custom positioned toast!', null, [
            'position' => 'bottom-left',
            'duration' => 0, // sticky — manual dismiss only
        ]);
    }

    public function render()
    {
        return view('livewire.profile-form');
    }
}

In Controllers (Toast Facade)

Use the Toast facade for controller-based workflows:

<?php

namespace App\Http\Controllers;

use Haxneeraj\LivewireToaster\Facades\Toast;
use App\Http\Requests\UserRequest;

class UserController extends Controller
{
    public function store(UserRequest $request)
    {
        // ... create user ...

        Toast::success('User created successfully!', 'Welcome');

        return redirect()->route('users.index');
    }

    public function destroy(User $user)
    {
        $user->delete();

        Toast::error('User has been removed.', 'Deleted');

        return redirect()->route('users.index');
    }
}

Note: When using the facade with redirects, register the InjectToasts middleware (see Middleware Setup).

Global Helper Function

// Quick toast
toast('success', 'Item saved!');

// Get the manager and chain
toast()->success('First toast')->error('Second toast');

// With options
toast('warning', 'Low disk space!', 'Warning', ['duration' => 10000]);

From JavaScript

Include resources/js/toast.js in your build, or use the global window.Toast object (available when the hub is loaded):

// These work anywhere in your JS
Toast.success('Saved!');
Toast.error('Something went wrong.', 'Error');
Toast.info('FYI...', null, { duration: 5000 });
Toast.warning('Careful!', 'Warning');
Toast.show('success', 'Custom type toast');

Or dispatch a raw event:

window.dispatchEvent(new CustomEvent('toast', {
    detail: {
        type: 'success',
        message: 'Hello from JS!',
        title: 'Custom Event',
        duration: 3000,
    }
}));

In Alpine.js

<button
    type="button"
    x-data
    x-on:click="$dispatch('toast', {
        type: 'success',
        message: 'Button clicked!',
        title: 'Action'
    })"
>
    Click me
</button>

⚙️ Configuration

Publish the config file:

php artisan vendor:publish --tag=toaster-config

Configuration Reference

Key Type Default Description
position string "top-right" Toast position. Options: top-right, top-left, bottom-right, bottom-left, top-center, bottom-center
duration int 3000 Auto-dismiss time in ms. 0 = sticky (manual dismiss only)
closable bool true Show close (×) button
close_on_click bool true Click anywhere on toast to dismiss
pause_on_hover bool true Pause auto-dismiss when hovering
max_toasts int 5 Max visible toasts. 0 = unlimited
queue bool false Queue mode: show one at a time
suppress_duplicates bool false Ignore duplicate type+message
replace_duplicates bool false Replace existing duplicate
show_icon bool true Show type-specific icon
show_progress_bar bool true Show animated progress bar
event_name string "toast" Browser event name
styles array see below Per-type Tailwind CSS classes

Style Configuration

Each toast type has customizable Tailwind classes:

'styles' => [
    'success' => [
        'bg'          => 'bg-emerald-50 dark:bg-emerald-950/80',
        'border'      => 'border-emerald-200 dark:border-emerald-800',
        'text'        => 'text-emerald-800 dark:text-emerald-200',
        'icon_color'  => 'text-emerald-500 dark:text-emerald-400',
        'progress_bg' => 'bg-emerald-500',
    ],
    // ... error, warning, info
],

🔧 Middleware Setup

To use the Toast facade or toast() helper in controllers (especially with redirects), register the InjectToasts middleware.

Laravel 11+ (bootstrap/app.php)

use Haxneeraj\LivewireToaster\Http\Middleware\InjectToasts;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            InjectToasts::class,
        ]);
    })
    ->create();

Laravel 10 (app/Http/Kernel.php)

protected $middlewareGroups = [
    'web' => [
        // ... other middleware ...
        \Haxneeraj\LivewireToaster\Http\Middleware\InjectToasts::class,
    ],
];

The middleware:

  1. Collects pending toasts from ToastManager and session flash
  2. Injects a <script> tag before </body> that dispatches window events
  3. The Alpine toast-hub catches these events and renders the toasts

🎨 Customizing Views

Publish the views to fully customize the toast markup:

php artisan vendor:publish --tag=toaster-views

This copies the Blade component to resources/views/vendor/livewire-toaster/.

Tailwind CSS Purge

Add the package views to your Tailwind content paths:

// tailwind.config.js
module.exports = {
    content: [
        './resources/views/**/*.blade.php',
        './vendor/haxneeraj/livewire4-toaster/resources/views/**/*.blade.php',
        // ...
    ],
};

🧪 Testing

Running Tests

composer install
vendor/bin/phpunit

Testing Your Toasts

When testing Livewire components that use the Toastable trait:

use Livewire\Livewire;

public function test_profile_save_shows_success_toast()
{
    Livewire::test(ProfileForm::class)
        ->set('name', 'John')
        ->call('save')
        ->assertDispatched('toast');
}

When testing controllers that use the facade:

use Haxneeraj\LivewireToaster\Facades\Toast;
use Haxneeraj\LivewireToaster\ToastManager;

public function test_user_creation_flashes_toast()
{
    $this->post('/users', ['name' => 'John', 'email' => 'john@example.com']);

    $manager = app(ToastManager::class);
    // Toasts are flushed by middleware, so check session for redirect tests
    $this->assertNotNull(session('livewire_toaster'));
}

📖 API Reference

Toastable Trait Methods

Method Signature Description
success() success(string $message, ?string $title = null, array $options = []) Green success toast
error() error(string $message, ?string $title = null, array $options = []) Red error toast
info() info(string $message, ?string $title = null, array $options = []) Blue info toast
warning() warning(string $message, ?string $title = null, array $options = []) Amber warning toast
toast() toast(string $type, string $message, ?string $title = null, array $options = []) Generic toast

Toast Facade / Manager Methods

Same methods as above, plus:

Method Description
toArray() Get all pending toasts
flush() Get and clear pending toasts
flashToSession() Flash toasts to session (for redirects)
hasPending() Check if toasts are queued
count() Count pending toasts

Per-Toast Options

Pass as the $options array:

$this->success('Done!', 'OK', [
    'duration' => 5000,      // Override duration (ms)
    'position' => 'top-left', // Override position
]);

📁 Package Structure

livewire4-toaster/
├── config/
│   └── toaster.php              # Published config
├── resources/
│   ├── js/
│   │   └── toast.js             # JS helper (window.Toast)
│   └── views/
│       └── components/
│           └── toast-hub.blade.php  # Alpine.js toast container
├── src/
│   ├── Facades/
│   │   └── Toast.php            # Laravel facade
│   ├── Http/
│   │   └── Middleware/
│   │       └── InjectToasts.php # Middleware for controller support
│   ├── Traits/
│   │   └── Toastable.php        # Livewire trait
│   ├── ToastManager.php         # Core toast collector
│   ├── ToasterServiceProvider.php # Service provider
│   └── helpers.php              # Global toast() helper
├── tests/
│   ├── Unit/
│   │   ├── InjectToastsMiddlewareTest.php
│   │   ├── ServiceProviderTest.php
│   │   └── ToastManagerTest.php
│   └── TestCase.php
├── CHANGELOG.md
├── LICENSE
├── README.md
├── composer.json
└── phpunit.xml

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing)
  5. Open a Pull Request

Please ensure tests pass (vendor/bin/phpunit) and follow PSR-12 coding standards.

📝 License

The MIT License (MIT). See LICENSE for details.

🙏 Credits

haxneeraj/laravel-livewire4-toaster 适用场景与选型建议

haxneeraj/laravel-livewire4-toaster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「notifications」 「laravel」 「toaster」 「alpine」 「toast」 「livewire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 haxneeraj/laravel-livewire4-toaster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 haxneeraj/laravel-livewire4-toaster 我们能提供哪些服务?
定制开发 / 二次开发

基于 haxneeraj/laravel-livewire4-toaster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-31