hinet/laravel-sms
laravel-sms
基于Laravel与Lumen的短信发送网关,支持阿里云短信、腾讯云短信、百度云短信、SendCloud;极易扩展,你可以自由定义自已的网关驱动。
安装
composer require hinet/laravel-sms
配置
- 注册服务提供器 在config/app.php文件中providers数组里加入:
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为表单中的手机号字段名
]);
Testing
拷贝单元测试文件SmsUnitTest.php到根目录tests文件中',并在命令行执行:
vendor\bin\phpunit tests\SmsUnitTest.php
Related Packages
saleemepoch/txtnation
A Laravel library to help send free and premium SMS via txtNation. Forked from M...
147
1
rinvex/laravel-authy
Rinvex Authy is a simple wrapper for Authy TOTP, the best rated Two-Factor Authe...
78,344
32
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.4.2 | ^7.2.5| | ^5.5| | MIT | |
| 1.4.1 | ^7.2.5 | ^5.5| | MIT | |
| 1.4.0 | ^7.2.5 | ^5.5| | MIT | |
| 1.3.9 | ^7.2.5 | ^5.5| | MIT | |
| 1.3.8 | ^7.2.5 | ^5.5| | MIT | |
| 1.3.7 | ^7.2.5 | ^5.5| | MIT | |
| 1.3.6 | ^7.2.5 | ^5.5| | MIT | |
| 1.3.0 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.9 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.8 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.7 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.6 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.5 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.4 | ^7.2.5 | ^5.5| | MIT | |
| 1.2.3 | >=7.0 | 5.8.* | MIT | |
| 1.2.2 | >=7.0 | 5.8.* | MIT | |
| 1.2.1 | >=7.0 | 5.8.* | MIT | |
| 1.2.0 | >=5.6 | ~5.3 | MIT | |
| 1.1.2 | >=5.6 | ~5.3 | MIT | |
| 1.1.1 | >=5.6 | ~5.3 | MIT | |
| 1.1.0 | >=5.6 | ~5.3 | MIT | |
| 1.0.3 | >=5.6 | ~5.3 | MIT | |
| 1.0.2 | >=5.6 | ~5.3 | MIT | |
| 1.0.0 | >=5.6 | ~5.3 | MIT |