jlorente / laravel-connectus

Laravel 对 Jlorente Connectus php SDK 的集成。

1.0.2 2021-08-10 09:00 UTC

This package is auto-updated.

Last update: 2024-09-10 15:58:50 UTC


README

Laravel 对 Connectus SDK 的集成,包括一个通知通道。

安装

安装此扩展的首选方式是通过 composer

安装 Composer 后,您可以使用以下命令安装扩展

$ php composer.phar require jlorente/laravel-connectus

或者在您的 composer.json 文件的 require 部分添加以下内容

...
    "require": {
        "jlorente/laravel-connectus": "*"
    }

配置

  1. 在您的 config/app.php 中的服务提供者列表中注册 ServiceProvider。

config/app.php

return [
    //other stuff
    'providers' => [
        //other stuff
        \Jlorente\Laravel\Connectus\ConnectusServiceProvider::class,
    ];
];
  1. 在 $aliases 部分添加以下外观。

config/app.php

return [
    //other stuff
    'aliases' => [
        //other stuff
        'Connectus' => \Jlorente\Laravel\Connectus\Facades\Connectus::class,
    ];
];
  1. 发布包配置文件。
$ php artisan vendor:publish --provider='Jlorente\Laravel\Connectus\ConnectusServiceProvider'
  1. 在 config/connectus.php 文件中设置 api_key 和 api_secret,或使用预定义的 env 变量。

config/connectus.php

return [
    'api_key' => 'YOUR_API_KEY',
    'account_id' => 'YOUR_ACCOUNT_ID',
    //other configuration
];

或 .env

//other configurations
CONNECTUS_API_KEY=<YOUR_API_KEY>
CONNECTUS_ACCOUNT_ID=<YOUR_ACCOUNT_ID>

使用

您可以使用外观别名 Connectus 执行 API 调用。认证参数将自动注入。

Connectus::api()->checkSmsBalance();

通知通道

本包包含一个通知通道,允许您集成 Connectus 发送短信服务。

有关 Laravel 通知的更多信息,请参阅 此页面

ConnectusSmsChannel

如果您想通过 Connectus 发送短信,应在通知类上定义 toConnectusSms 方法。此方法将接收一个 $notifiable 实体,并应返回要发送到短信的消息字符串。

/**
 * Get the SMS message.
 *
 * @param  mixed  $notifiable
 * @return string
 */
public function toConnectusSms($notifiable)
{
    return 'Hello, this is an SMS sent through Connectus API';
}

完成之后,必须在通知的 via() 方法的数组中添加通知通道

/**
 * Get the notification channels.
 *
 * @param  mixed  $notifiable
 * @return array|string
 */
public function via($notifiable)
{
    return [ConnectusSmsChannel::class];
}

通知路由

当通过 Connectus 通道发送通知时,通知系统会自动在 notifiable 实体上查找 phone_number 属性。如果您想自定义号码,应在实体上定义 routeNotificationForConnectusSms 方法

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the Connectus SMS channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForConnectusSms($notification)
    {
        return $this->phone;
    }
}

许可证

版权所有 © 2021 José Lorente Martín jose.lorente.martin@gmail.com

根据 BSD 3-Clause 许可证授权。有关详细信息,请参阅 LICENSE.txt。