Laravel框架下实现用户提交手机号后发送APP下载链接短信(仿Groupon)
Hey there! I’ve built similar features for Laravel apps before, so let’s walk through exactly how to set this up—from capturing the user’s phone number to sending that download link via SMS, just like Groupon’s flow.
1. Pick an SMS Service Provider & Integrate with Laravel
Laravel doesn’t handle SMS natively, so you’ll need a third-party service. Popular options include Twilio, Plivo, or region-specific providers like Alibaba Cloud SMS (if targeting China). Let’s use Twilio as an example since it has seamless Laravel support:
- Install the Twilio notification channel package:
composer require laravel-notification-channels/twilio - Add your Twilio credentials to
.env:TWILIO_AUTH_TOKEN=your_auth_token TWILIO_ACCOUNT_SID=your_account_sid TWILIO_FROM_NUMBER=your_twilio_phone_number - (Optional) Publish the config file for customization:
php artisan vendor:publish --provider="NotificationChannels\Twilio\TwilioServiceProvider"
2. Build the Phone Number Submission Form & Route
Create a simple frontend form to capture the user’s phone number, plus a backend route to handle the submission:
Frontend Blade Form Example
<form method="POST" action="{{ route('send.download.link') }}"> @csrf <div class="form-group"> <label for="phone">Enter your phone number:</label> <input type="tel" id="phone" name="phone" required> <small class="text-muted">e.g., +1234567890 for international, or 1234567890 for local (adjust validation as needed)</small> </div> <button type="submit" class="btn btn-primary">Get App Download Link</button> </form>
Backend Route (routes/web.php)
use App\Http\Controllers\SmsDownloadController; Route::post('/send-download-link', [SmsDownloadController::class, 'send'])->name('send.download.link');
3. Create a Controller to Handle Submission & Validation
Generate a controller to validate the phone number and trigger the SMS:
php artisan make:controller SmsDownloadController
Add the core logic to the controller:
<?php namespace App\Http\Controllers; use App\Notifications\SendAppDownloadLink; use Illuminate\Http\Request; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Log; class SmsDownloadController extends Controller { public function send(Request $request) { // Validate phone number (use E.164 format for global delivery) $validated = $request->validate([ 'phone' => ['required', 'regex:/^\+?[1-9]\d{1,14}$/'], ]); // Define your app download links (split into iOS/Android if needed) $downloadLinks = [ 'android' => 'https://your-app-domain.com/android.apk', 'ios' => 'https://apps.apple.com/us/app/your-app-name/id1234567890' ]; // Craft the SMS message (use short links to keep it concise!) $message = "Thanks for requesting our app! Download here:\nAndroid: {$downloadLinks['android']}\niOS: {$downloadLinks['ios']}"; // Send SMS with error handling try { Notification::route('twilio', $validated['phone']) ->notify(new SendAppDownloadLink($message)); return back()->with('success', 'Download link sent to your phone!'); } catch (\Exception $e) { Log::error("SMS delivery failed: {$e->getMessage()}"); return back()->with('error', 'Oops! We couldn’t send your link. Please try again later.'); } } }
4. Create the SMS Notification Class
Laravel’s Notification system simplifies sending SMS. Generate a notification class:
php artisan make:notification SendAppDownloadLink
Update it to use Twilio:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\TwilioMessage; use Illuminate\Notifications\Notification; class SendAppDownloadLink extends Notification { use Queueable; protected $message; public function __construct(string $message) { $this->message = $message; } public function via(object $notifiable): array { return ['twilio']; } public function toTwilio(object $notifiable): TwilioMessage { return (new TwilioMessage)->content($this->message); } }
5. Production Best Practices
- Shorten Links: Long URLs can clutter SMS or get truncated. Use a package like
ashallendesign/short-urlto create clean short links for your download URLs. - Compliance: Follow local regulations (e.g., TCPA in the US, GDPR in EU) — ensure users opt in to receive messages from you.
- Queue SMS: For high traffic, queue the notification to avoid slowing down your app. Add
ShouldQueueto the notification class and set up Laravel queues. - Test Thoroughly: Use your SMS provider’s test credentials to send free test messages and validate delivery.
内容的提问来源于stack exchange,提问作者NoobLearner

