定制 sangamkatwal/sangam-toastr 二次开发

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

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

sangamkatwal/sangam-toastr

Composer 安装命令:

composer require sangamkatwal/sangam-toastr

包简介

A simple Laravel package for Toastr notifications with support for session flash messages and AJAX responses. Handles success, error, warning, and info notifications seamlessly.

README 文档

README

Latest Version License

A simple and elegant Laravel package for Toastr notifications that works seamlessly with both session flash messages and AJAX responses.

Note: This is my first Laravel package! 🚀 If anything isn't clear, feel free to reach out via email or create an issue.

✨ Features

  • 🔄 Dual Support: Works with both session redirects and AJAX calls
  • 🎨 4 Notification Types: Success, Error, Warning, Info
  • 🚀 Zero Configuration: Works out of the box
  • 📱 Responsive: Mobile-friendly notifications
  • 🔧 Laravel Integration: Auto-discovery support
  • 💡 Simple API: Consistent key-value pair format

📦 Installation

composer require sangamkatwal/sangam-toastr

🔧 Setup

1. Publish Assets

# Publish everything
php artisan vendor:publish --tag=sangam-toastr

# Or publish separately
php artisan vendor:publish --tag=sangam-toastr-assets
php artisan vendor:publish --tag=sangam-toastr-views

2. Include Assets in Layout

Add to your layouts/app.blade.php (inside <head>):

{{-- jQuery (required) --}}
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

{{-- Toastr Assets --}}
<link rel="stylesheet" href="{{ asset('vendor/sangam-toastr/toastr.min.css') }}">
<script src="{{ asset('vendor/sangam-toastr/toastr.min.js') }}"></script>

3. Include Notifications

Add before closing </body> tag in your layout:

@include('sangam-toastr::toastr')

🚀 Usage

Session Flash Messages (Redirects)

// Success notification
return redirect()->back()->with([
    'success' => true,
    'message' => 'Data saved successfully!'
]);

// Error notification
return redirect()->back()->with([
    'error' => true,
    'message' => 'Something went wrong!'
]);

// Multiple messages (arrays supported)
return redirect()->back()->with([
    'error' => true,
    'message' => ['Field is required', 'Email must be valid']
]);

AJAX Responses

// Success response
return response()->json([
    'success' => true,
    'message' => 'Operation completed!'
]);

// Error response
return response()->json([
    'error' => true,
    'message' => 'Validation failed!'
]);

Frontend AJAX Implementation

$.ajax({
    url: '/your-endpoint',
    type: 'POST',
    data: formData,
    success: function(response) {
        // This will automatically show toastr notifications
        ajax_response(response);
        
        // Alternative function name
        // showToastr(response);
    },
    error: function(xhr) {
        // Handle server errors
        ajax_response({
            error: true,
            message: 'Server error occurred'
        });
    }
});

Mixed Request Types (AJAX + Regular)

Perfect for controllers that handle both AJAX and regular requests:

public function store(Request $request)
{
    try {
        // Your logic here...
        
        $message = 'Record created successfully!';
        
        return $request->ajax()
            ? response()->json(['success' => true, 'message' => $message])
            : redirect()->back()->with(['success' => true, 'message' => $message]);
            
    } catch (Exception $e) {
        $message = 'Something went wrong!';
        
        return $request->ajax()
            ? response()->json(['error' => true, 'message' => $message])
            : redirect()->back()->with(['error' => true, 'message' => $message]);
    }
}

📋 Notification Types

Type Usage
success ✅ Success operations
error ❌ Error messages
warning ⚠️ Warning alerts
info ℹ️ Information notices

⚙️ Requirements

  • PHP: >= 8.0
  • Laravel: 8.x, 9.x, 10.x, 11.x
  • jQuery: 3.7.1 (recommended, not tested with other versions)

🔍 Validation Examples

See ValidationExamples.md for detailed examples with Laravel validation.

🤝 Contributing

This is my first package, so contributions and suggestions are very welcome!

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

📝 License

This package is open-sourced under the MIT license.

👨‍💻 Author

Sangam Katwal - katwalsangam@gmail.com

✍️ Note By Author

This README was written with the help of AI based on my inputs. If you notice anything unclear or incorrect, feel free to reach out to me via email.

The Toastr CSS and JS used in this package are from the original Toastr.js v2.1.3. This package is completely free and open-source.

The main reason I built this package is because I often had to dig through old projects just to copy my custom Toastr response logic. By turning it into a reusable package, I’ve made it easier for myself—and I’m sharing it publicly in case it simplifies things for others too.

👨‍💻 Note For Developers

While this package encourages a simple and consistent response format (one notification type + one message), it does not restrict Laravel’s default flexibility. You can still attach additional values to your responses, whether for session flashes or AJAX.

For example:

// Redirect response with extra data
return redirect()->back()->with([
    'success' => true,
    'message' => 'Data saved successfully!',
    'status'  => 200,
    'extra'   => 'Any other value'
]);

// AJAX response with extra fields
return response()->json([
    'success' => true,
    'message' => 'Data saved successfully!',
    'status'  => 200,
    'data'    => ['id' => 123, 'name' => 'Sample']
]);

So, you still get all the power of Laravel responses while enjoying clean, consistent Toastr notifications. 🚀

---

⭐ **Star this repo if it helped you!**

sangamkatwal/sangam-toastr 适用场景与选型建议

sangamkatwal/sangam-toastr 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 sangamkatwal/sangam-toastr 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-26