SMS gateway package for laravel,supports Alibaba cloud SMS, Tencent cloud SMS, SendCloud SMS and Baidu cloud SMS. It is easy to expand and you can freely define your own gateway driver.
hinet/laravel-sms is a Laravel package for sms gateway package for laravel,supports alibaba cloud sms, tencent cloud sms, sendcloud sms and baidu cloud sms. it is easy to expand and you can freely define your own gateway driver..
It currently has 5 GitHub stars and 419 downloads on Packagist (latest version 1.4.2).
Install it with composer require hinet/laravel-sms.
Discover more Laravel packages by hinet
or browse all Laravel packages to compare alternatives.
Last updated
基于Laravel与Lumen的短信发送网关,支持阿里云短信、腾讯云短信、百度云短信、SendCloud;极易扩展,你可以自由定义自已的网关驱动。
composer require hinet/laravel-sms
Hinet\Sms\SmsServiceProvider::class,
在config/app.php文件中的aliases数组里加入
'Sms' => Hinet\Sms\Facades\Sms::class,
php artisan vendor:publish --tag=smsconfig
在web路由中,可以使用session或cache来存储状态;在api路由需要使用cache存储,因为api中间件中不含session会话支持
//config/sms.php
//存储器
'storage' => [
'prefix' => '',//存储key的前缀
'driver' => 'cache',//存储方式,内置可选的值有'session'和'cache',api路由中请使用cache
]
| 平台 | 网关名称 | | --- | --- | | 阿里云 | aliyun | | 腾讯云 | qcloud | | 百度云 | baidu | | 聚合 | juhe | | sendcloud | sendcloud |
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sms;
class HomeController extends Controller
{
public function index(){
$gateway = Sms::gateway('aliyun');
//发送
echo $gateway->send('测试手机号码');
//校验码是否正确
echo $gateway->verifyCode('测试手机号码','验证码');
}
}
打开app\Providers\AppServiceProvider.php文件,在boot()方法中添加:
//use Illuminate\Support\Facades\Validator;
Validator::extend('verify_sms', function ($attribute, $value, $parameters) {
$mobile = app('request')->input($parameters[0]);
$gateway = \Sms::gateway(config('sms.default'));
return $gateway->verifyCode($mobile,$value);
});
验证示例:
$validator = Validator::make($data, [
'phone' => 'unique:表名',
'verifyCode' => 'verify_sms:phone',//verifyCode为表单中的验证码名称,verify_sms为短信验证方法名,phone为表单中的手机号字段名
]);
拷贝单元测试文件SmsUnitTest.php到根目录tests文件中',并在命令行执行:
vendor\bin\phpunit tests\SmsUnitTest.php