rezak/laravel-sms-channel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

rezak/laravel-sms-channel

Composer 安装命令:

composer require rezak/laravel-sms-channel

包简介

A Laravel package for sending SMS notifications using various services.

README 文档

README

A Laravel package that provides a flexible SMS channel integration supporting multiple SMS services, including Kavenegar, to send SMS notifications.

Installation

You can install the package via Composer:

composer require rezak/laravel-sms-channel:dev-master

Publishing Vendor Files

After installing, publish the configuration file by running the following command:

php artisan vendor:publish --provider="Rezak\\SMSNotification\\Providers\\SMSNotificationServiceProvider"

This command will publish the package's configuration files into your Laravel project, allowing you to customize the SMS service settings as needed.

Configuration

Add the following to your config/services.php:

<?php

return [
    'default' => env('SMS_DRIVER', 'mock'),
    'drivers' => [
        'mock' => [
            'class' => \\Rezak\\SMSNotification\\Services\\SMSService\\MockSMSService::class,
        ],
        'kavenegar' => [
            'class' => \\Rezak\\SMSNotification\\Services\\SMSService\\KavenegarSMSService::class,
            'token' => env('KAVENEGAR_API_TOKEN'),
            'sender'=> "10004346"
        ]
    ],
];

Usage

1. Add the routeNotificationForSMS method to your User model

In order to use the SMS channel for notifications, you need to add the following method to your User model:

public function routeNotificationForSMS()
{
    return $this->phone;
}

This method should return the phone number of the user.

2. Create a Notification

Create a notification class to send an OTP (or any SMS message). Here's an example:

<?php

namespace App\\Notifications;

use Illuminate\\Notifications\\Notification;
use Rezak\\SMSNotification\\Messages\\SMSMessage;

class SendOtpNotification extends Notification
{
    public function via(object $notifiable): array
    {
        return ['SMS'];
    }

    public function toSMS(object $notifiable): SMSMessage
    {
        $message = new SMSMessage();
        $message->setTemplate('test_template')
                ->setData(['param1' => 'value1']);

        return $message;
    }
}

3. Sending the Notification

Send the notification to a specific user:

$user = User::find(1);
$otpCode = rand(100000, 999999);
$user->notify(new SendOtpNotification($otpCode));

Alternatively, send the notification to multiple phone numbers:

use Illuminate\\Support\\Facades\\Notification;

Notification::route('SMS', ['first_phone', 'second_phone'])
    ->notify(new SendOtpNotification('otp code'));

4. Customizing the SMS Message

Use predefined templates (via setTemplate()) or send custom content (via setContent()).

Example:

$message = new SMSMessage();
$message->setContent('This is a test message.');
return $message;

Supported SMS Services

  • Kavenegar: API integration for sending SMS messages.

You can extend the package to support more SMS services by adding the appropriate driver.

Environment Configuration

Make sure to add your API key to the .env file:

KAVENEGAR_API_KEY=your-api-key

License

This package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-12