从0到1使用Laravel Vonage Notification Channel构建用户注册短信验证系统【免费下载链接】vonage-notification-channelVonage Notification Channel for Laravel.项目地址: https://gitcode.com/gh_mirrors/vo/vonage-notification-channel在现代Web应用中用户注册短信验证是保障账户安全的重要环节。本文将带你一步步使用Laravel Vonage Notification Channel构建一个完整的用户注册短信验证系统无需复杂的第三方服务集成让你轻松实现专业级的短信验证功能。 准备工作环境与依赖在开始之前请确保你的开发环境满足以下条件Laravel 8.x 或更高版本PHP 7.4 或更高版本Composer 包管理工具Vonage原Nexmo账户及API凭证首先通过Composer安装Laravel Vonage Notification Channel包composer require laravel/vonage-notification-channel安装完成后服务提供商会自动注册。如果你需要手动注册可以在config/app.php文件的providers数组中添加Illuminate\Notifications\VonageChannelServiceProvider::class, Vonage账户配置获取API凭证访问Vonage官网注册并登录账户在 dashboard 中创建新应用获取API Key和API Secret申请一个短信发送号码SMS From Number配置环境变量打开项目根目录的.env文件添加以下配置VONAGE_KEYyour_api_key VONAGE_SECRETyour_api_secret VONAGE_SMS_FROMyour_vonage_phone_number配置文件位于config/vonage.php你可以根据需要修改默认配置。该文件包含了短信发送号码、API凭证、签名密钥等重要配置项。 创建用户模型与迁移首先创建用户模型和迁移文件如果尚未创建php artisan make:model User -m在迁移文件中添加手机号和验证字段Schema::create(users, function (Blueprint $table) { $table-id(); $table-string(name); $table-string(email)-unique(); $table-string(phone)-unique(); $table-string(verification_code); $table-boolean(phone_verified)-default(false); $table-timestamps(); });运行迁移php artisan migrate 创建通知类使用Artisan命令创建短信验证通知php artisan make:notification SmsVerificationNotification编辑生成的通知类文件app/Notifications/SmsVerificationNotification.phpuse Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\VonageMessage; class SmsVerificationNotification extends Notification { private $code; public function __construct($code) { $this-code $code; } public function via($notifiable) { return [vonage]; } public function toVonage($notifiable) { return (new VonageMessage) -content(您的验证码是: {$this-code}有效期10分钟。); } } 实现发送与验证逻辑生成验证码在User模型中添加生成验证码的方法public function generateVerificationCode() { $this-verification_code rand(100000, 999999); $this-save(); return $this-verification_code; }发送验证码创建注册控制器并添加发送验证码的方法use App\Models\User; use App\Notifications\SmsVerificationNotification; class RegisterController extends Controller { public function sendVerificationCode(Request $request) { $request-validate([ phone required|unique:users ]); $user User::firstOrCreate( [phone $request-phone], [name 新用户, email uniqid().example.com] ); $code $user-generateVerificationCode(); $user-notify(new SmsVerificationNotification($code)); return response()-json([message 验证码已发送]); } }验证验证码添加验证验证码的方法public function verifyCode(Request $request) { $request-validate([ phone required, code required|digits:6 ]); $user User::where(phone, $request-phone) -where(verification_code, $request-code) -first(); if (!$user) { return response()-json([message 验证码错误], 400); } $user-phone_verified true; $user-save(); return response()-json([message 验证成功]); } 配置路由在routes/api.php中添加路由Route::post(send-verification-code, [RegisterController::class, sendVerificationCode]); Route::post(verify-code, [RegisterController::class, verifyCode]);✅ 测试验证流程现在你可以使用Postman或其他API测试工具测试整个验证流程发送POST请求到/api/send-verification-code参数phone你的手机号接收短信验证码发送POST请求到/api/verify-code参数phone你的手机号code收到的验证码⚙️ 高级配置与优化自定义短信内容你可以在config/vonage.php中配置默认的短信发送者信息或在通知类中自定义短信内容和发送者。处理发送失败添加异常处理确保在短信发送失败时能够妥善处理try { $user-notify(new SmsVerificationNotification($code)); } catch (\Exception $e) { // 记录日志或通知用户 Log::error(短信发送失败: . $e-getMessage()); return response()-json([message 短信发送失败请稍后重试], 500); }验证码过期时间添加验证码过期逻辑提高安全性// 在users表中添加verification_code_expires_at字段 public function generateVerificationCode() { $this-verification_code rand(100000, 999999); $this-verification_code_expires_at now()-addMinutes(10); $this-save(); return $this-verification_code; } // 验证时检查是否过期 $user User::where(phone, $request-phone) -where(verification_code, $request-code) -where(verification_code_expires_at, , now()) -first(); 总结通过本文的教程你已经成功使用Laravel Vonage Notification Channel构建了一个完整的用户注册短信验证系统。这个系统包括了验证码生成、发送、验证等核心功能并且可以根据实际需求进行扩展和优化。Laravel Vonage Notification Channel提供了简单而强大的API让短信通知的集成变得轻松愉快。无论是用户验证、订单通知还是系统警报它都能满足你的需求。希望本文对你有所帮助如果有任何问题或建议请随时在评论区留言。祝你开发顺利【免费下载链接】vonage-notification-channelVonage Notification Channel for Laravel.项目地址: https://gitcode.com/gh_mirrors/vo/vonage-notification-channel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
从0到1:使用Laravel Vonage Notification Channel构建用户注册短信验证系统
发布时间:2026/7/4 21:17:32
从0到1使用Laravel Vonage Notification Channel构建用户注册短信验证系统【免费下载链接】vonage-notification-channelVonage Notification Channel for Laravel.项目地址: https://gitcode.com/gh_mirrors/vo/vonage-notification-channel在现代Web应用中用户注册短信验证是保障账户安全的重要环节。本文将带你一步步使用Laravel Vonage Notification Channel构建一个完整的用户注册短信验证系统无需复杂的第三方服务集成让你轻松实现专业级的短信验证功能。 准备工作环境与依赖在开始之前请确保你的开发环境满足以下条件Laravel 8.x 或更高版本PHP 7.4 或更高版本Composer 包管理工具Vonage原Nexmo账户及API凭证首先通过Composer安装Laravel Vonage Notification Channel包composer require laravel/vonage-notification-channel安装完成后服务提供商会自动注册。如果你需要手动注册可以在config/app.php文件的providers数组中添加Illuminate\Notifications\VonageChannelServiceProvider::class, Vonage账户配置获取API凭证访问Vonage官网注册并登录账户在 dashboard 中创建新应用获取API Key和API Secret申请一个短信发送号码SMS From Number配置环境变量打开项目根目录的.env文件添加以下配置VONAGE_KEYyour_api_key VONAGE_SECRETyour_api_secret VONAGE_SMS_FROMyour_vonage_phone_number配置文件位于config/vonage.php你可以根据需要修改默认配置。该文件包含了短信发送号码、API凭证、签名密钥等重要配置项。 创建用户模型与迁移首先创建用户模型和迁移文件如果尚未创建php artisan make:model User -m在迁移文件中添加手机号和验证字段Schema::create(users, function (Blueprint $table) { $table-id(); $table-string(name); $table-string(email)-unique(); $table-string(phone)-unique(); $table-string(verification_code); $table-boolean(phone_verified)-default(false); $table-timestamps(); });运行迁移php artisan migrate 创建通知类使用Artisan命令创建短信验证通知php artisan make:notification SmsVerificationNotification编辑生成的通知类文件app/Notifications/SmsVerificationNotification.phpuse Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\VonageMessage; class SmsVerificationNotification extends Notification { private $code; public function __construct($code) { $this-code $code; } public function via($notifiable) { return [vonage]; } public function toVonage($notifiable) { return (new VonageMessage) -content(您的验证码是: {$this-code}有效期10分钟。); } } 实现发送与验证逻辑生成验证码在User模型中添加生成验证码的方法public function generateVerificationCode() { $this-verification_code rand(100000, 999999); $this-save(); return $this-verification_code; }发送验证码创建注册控制器并添加发送验证码的方法use App\Models\User; use App\Notifications\SmsVerificationNotification; class RegisterController extends Controller { public function sendVerificationCode(Request $request) { $request-validate([ phone required|unique:users ]); $user User::firstOrCreate( [phone $request-phone], [name 新用户, email uniqid().example.com] ); $code $user-generateVerificationCode(); $user-notify(new SmsVerificationNotification($code)); return response()-json([message 验证码已发送]); } }验证验证码添加验证验证码的方法public function verifyCode(Request $request) { $request-validate([ phone required, code required|digits:6 ]); $user User::where(phone, $request-phone) -where(verification_code, $request-code) -first(); if (!$user) { return response()-json([message 验证码错误], 400); } $user-phone_verified true; $user-save(); return response()-json([message 验证成功]); } 配置路由在routes/api.php中添加路由Route::post(send-verification-code, [RegisterController::class, sendVerificationCode]); Route::post(verify-code, [RegisterController::class, verifyCode]);✅ 测试验证流程现在你可以使用Postman或其他API测试工具测试整个验证流程发送POST请求到/api/send-verification-code参数phone你的手机号接收短信验证码发送POST请求到/api/verify-code参数phone你的手机号code收到的验证码⚙️ 高级配置与优化自定义短信内容你可以在config/vonage.php中配置默认的短信发送者信息或在通知类中自定义短信内容和发送者。处理发送失败添加异常处理确保在短信发送失败时能够妥善处理try { $user-notify(new SmsVerificationNotification($code)); } catch (\Exception $e) { // 记录日志或通知用户 Log::error(短信发送失败: . $e-getMessage()); return response()-json([message 短信发送失败请稍后重试], 500); }验证码过期时间添加验证码过期逻辑提高安全性// 在users表中添加verification_code_expires_at字段 public function generateVerificationCode() { $this-verification_code rand(100000, 999999); $this-verification_code_expires_at now()-addMinutes(10); $this-save(); return $this-verification_code; } // 验证时检查是否过期 $user User::where(phone, $request-phone) -where(verification_code, $request-code) -where(verification_code_expires_at, , now()) -first(); 总结通过本文的教程你已经成功使用Laravel Vonage Notification Channel构建了一个完整的用户注册短信验证系统。这个系统包括了验证码生成、发送、验证等核心功能并且可以根据实际需求进行扩展和优化。Laravel Vonage Notification Channel提供了简单而强大的API让短信通知的集成变得轻松愉快。无论是用户验证、订单通知还是系统警报它都能满足你的需求。希望本文对你有所帮助如果有任何问题或建议请随时在评论区留言。祝你开发顺利【免费下载链接】vonage-notification-channelVonage Notification Channel for Laravel.项目地址: https://gitcode.com/gh_mirrors/vo/vonage-notification-channel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考