beelab/phone-verification-bundle
Installation
composer require beelab/phone-verification-bundle
Add to config/bundles.php:
BeeLab\PhoneVerificationBundle\BeeLabPhoneVerificationBundle::class => ['all' => true],
Configuration Publish the default config:
php bin/console bee-lab:phone-verification:install
Edit config/packages/bee_lab_phone_verification.yaml to set:
service (Twilio, Nexmo, etc.)First Use Case Verify a phone number in a controller:
use BeeLab\PhoneVerificationBundle\Service\PhoneVerificationService;
class UserController extends AbstractController
{
public function verifyPhone(PhoneVerificationService $verifier)
{
$verifier->sendVerificationCode('+1234567890');
// Returns a UUID for tracking
}
}
Trigger Verification
$verifier->sendVerificationCode($phoneNumber);
// Returns UUID (e.g., 'a1b2c3d4')
Store the UUID in the user session or DB.
Verify Code
$verifier->verifyCode($phoneNumber, $userSubmittedCode, $uuid);
// Returns bool
Resend Code
$verifier->resendCode($phoneNumber, $uuid);
use BeeLab\PhoneVerificationBundle\Validator\Constraints\PhoneVerification;
#[PhoneVerification(message: 'Invalid verification code')]
public $verificationCode;
# config/packages/bee_lab_phone_verification.yaml
listeners:
BeeLab\PhoneVerificationBundle\EventListener\YourCustomListener: ['onVerificationSent']
$verifier->sendVerificationCodeAsync($phoneNumber);
| Scenario | Implementation Pattern |
|---|---|
| Signup flow | Verify phone after email registration. |
| Two-factor auth | Require code for sensitive actions. |
| Localization | Override country code per user region. |
| Testing | Mock the service in PHPUnit with TestService. |
Rate Limiting
RateLimitExceededException:
try {
$verifier->sendVerificationCode($phone);
} catch (RateLimitExceededException $e) {
$this->addFlash('error', 'Please wait before retrying.');
}
UUID Management
used_at timestamp to auto-cleanup old entries.Timeouts
verification_code_lifetime (default: 10 minutes).International Numbers
+447123456789).libphonenumber to validate/normalize numbers before passing to the bundle.Logs: Enable debug mode in config/packages/bee_lab_phone_verification.yaml:
debug: true
Logs appear in var/log/dev.log.
Test Mode: Simulate sends without actual SMS:
test_mode: true
Useful for development/testing.
Custom Services Override the default service (e.g., for a custom SMS provider):
services:
BeeLab\PhoneVerificationBundle\Service\PhoneVerificationService:
arguments:
$smsService: '@your.custom.sms_service'
Templates Customize SMS templates by extending the bundle’s twig templates:
{# templates/bee_lab_phone_verification/verification_code.txt.twig #}
Your verification code is: {{ code }}. Valid for {{ lifetime }} minutes.
Validation
Extend the PhoneVerification constraint to add custom rules:
class CustomPhoneVerification extends PhoneVerification
{
public $message = 'Custom error message.';
}
default_country_code: '+1' # US
%env() for credentials:
twilio:
account_sid: '%env(TWILIO_SID)%'
auth_token: '%env(TWILIO_TOKEN)%'
cache_verification_codes: false
How can I help you explore Laravel packages today?