Laravel 5.1 使用短信验证码插件laravel-sms
2016-07-02 17:57
537 查看
Laravel-sms 2.4
插件请戳这里:https://github.com/toplan/laravel-sms,该插件支持laravel 5.2+, 5.1版本的需要修改routes。安装
composer require 'toplan/laravel-sms:2.4.*' php artisan vendor:publish php artisan migrate
和该插件的开发者toplan沟通后,laravel 5.1下要使用该插件的方法是,
vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/routes.php // '注释掉这个中间件' //'middleware' => config('laravel-sms.middleware', 'web'),
配置
短信服务商
我使用的是luosimao。vi config/phpsms.php 'scheme' => [ 'Luosimao', ], */ 'Luosimao' => [ // 短信 API key // 在管理中心->短信->触发发送下查看 'apikey' => '你的apikey', // 语言验证 API key // 在管理中心->语音->语音验证下查看 'voiceApikey' => '你的voiceapi key', ],
验证码短信及有效时间
vi config/laravel-sms.php 'storage' => 'Toplan\Sms\SessionStorage', 'verifySmsContent' => '您的验证码是%s。有效期%s分钟,请尽快验证。【APP】', 'codeLength' => 5, 'codeValidMinutes' => 60,
使用
引入laravel-sms.js
该js绑定表单页面里的发送验证码按钮,并取得手机号输入框的内容。配置如下。我的表单中手机号input name为telephone,所以在下面如此指定。请求间隔我设置成了一个小时。$('#sendVerifySmsButton').sms({ //laravel csrf token token : "{{csrf_token()}}", //定义如何获取mobile的值 mobile_selector : 'input[name=telephone]', //手机号的检测规则 mobile_rule : 'mobile_required', //语音验证 voice : false, //请求间隔时间 interval : 3600 });
表单验证
我的前端表单是这样的<div class="form-group control-group"> {!! Form::label('telephone', '手机号码', ['class' => 'col-sm-2 control-label']) !!} <div class="controls col-sm-10"> {!! Form::text('telephone', $telephone, ['class' => 'form-control', 'required' => true, 'data-validation-required-message' => '请输入11位手机号 码', 'placeholder' => '请输入11位手机号码', 'pattern' => '^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$', 'data-validation-pattern-message' => '请输入正确的手机号码', 'maxlength' => '17']) !!} <br/> <button class="btn btn-highlight" id="sendVerifySmsButton" type="button"> 发送验证码 </button> </div> </div> <div class="form-group control-group"> {!! Form::label('vcode', '验证码', ['class' => 'col-sm-2 control-label']) !!} <div class="controls col-sm-10"> {!! Form::text('vcode', '', ['class' => 'form-control', 'required' => true, 'data-validation-required-message' => '请输入收到的验证码', 'placeholder' => '请输入验证码', 'pattern' => '^\d{5}$', 'data-validation-pattern-message' => '请>输入正确的验证码', 'maxlength' => '5']) !!} </div> </div>
如果你的表单验证控制写在这里,
vi app/Http/Requests/Frontend/PostFormRequest.php
添加验证方法如下,这几条验证规则由插件定义。
confirm_mobile_not_change,verify_code|confirm_rule:mobile,mobile_required
public function rules() { return [ ... 'telephone' => 'required|max:55|confirm_mobile_not_change', 'vcode' => 'required|verify_code|confirm_rule:mobile,mobile_required', ]; }
插件通过表单验证的逻辑实现验证码是否和手机号匹配,所以是很方便的。除此之外该插件还有队列及其他控制,很优秀。
增加黑名单控制
因为项目要求,需要增加手机号黑名单控制,我就把该插件稍微改了一下,给大家分享一下做法。给laravel-sms表增加一个标志位
创建数据库迁移文件php artisan make:migration add_blocked_to_laravel_sms_table
修改迁移动作
vi database/migrations/***_add_status_to_conferences_table.php
up里增加标志位项,down里移除。
public function up() { // Schema::table('laravel_sms', function ($table){ $table->integer('blocked')->default('0')->after('id')->comment('1 is blocked, 0 is not blocked'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::table('laravel_sms', function ($table){ $table->dropColumn('blocked'); }); }
阻止给已被禁止的手机号发送短信
先验证是否该手机号已被封禁。vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/SmsManager.php public function validateFields(array $data){ .... // check if blocked if(DB::table('laravel_sms')->where('to', $data['mobile'])->where('blocked', 1)->get()){ return self::generateResult(false, '已封禁'); }
创建拉黑和解除黑名单方法
创建routes并导入到这两个方法里面。public function blacklist($telephone){ //blacklist telephone DB::table('laravel_sms')->where('to', $telephone)->update(['blocked'=>1]); ... } public function undo_blacklist($telephone){ //undo blacklist telephone DB::table('laravel_sms')->where('to', $telephone)->update(['blocked'=>0]); ... }
防止跨域请求CSRF
这是个题外的,貌似原作的post表单里写了csrf_token(),但routes里面没看到使用,所以我把这个文件也修改了,作为备注。
vi vendor/toplan/laravel-sms/src/Toplan/LaravelSms/routes.php Route::post('verify-code', array('before'=>'csrf', 'uses' => 'Toplan\Sms\SmsController@postSendCode'));
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- java自动生成验证码插件-kaptcha
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- workerman结合laravel开发在线聊天应用的示例代码
- php7 读取php.ini[4]
- 简单对比分析Ruby on Rails 和 Laravel
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充