kavenegar/laravel
Laravel integration for Kavenegar SMS/voice API. Install via Composer, register the service provider and facade, publish the config, and set your Kavenegar API key. Then call the Kavenegar facade anywhere in your app to send messages.
Installation:
composer require kavenegar/laravel
Add to config/app.php:
'providers' => [
// ...
Kavenegar\Laravel\ServiceProvider::class,
],
'aliases' => [
'Kavenegar' => Kavenegar\Laravel\Facade::class,
]
Publish config:
php artisan vendor:publish --provider="Kavenegar\Laravel\ServiceProvider"
Configure API Key:
Edit .env or config/kavenegar.php:
KAVENEGAR_API_KEY=your_api_key_here
First Use Case: Send a basic SMS:
use Kavenegar\Facades\Kavenegar;
Kavenegar::send('1234567890', 'Hello from Laravel!');
Sending SMS:
Kavenegar::send($receiver, $message);
Kavenegar::send($receiver, $message, [
'type' => 'sms', // or 'dlr'
'line' => '1234567890', // sender number
]);
Verification Codes:
$code = Kavenegar::sendVerificationCode($receiver, [
'template' => 'verify_code',
'token' => '123456',
'callback' => 'http://example.com/callback',
]);
Async Sending:
Kavenegar::sendAsync($receiver, $message);
Queue Integration:
// Dispatch a job
SendSmsJob::dispatch($receiver, $message);
Define job:
use Kavenegar\Facades\Kavenegar;
use Illuminate\Bus\Queueable;
class SendSmsJob implements ShouldQueue {
use Queueable;
public function handle() {
Kavenegar::send($this->receiver, $this->message);
}
}
User Registration/Reset:
// In UserController@register
$code = Kavenegar::sendVerificationCode($user->phone, [
'template' => 'register_code',
'token' => $user->verification_token,
]);
Two-Factor Auth:
$code = Kavenegar::send($user->phone, "Your 2FA code: {$user->two_factor_code}");
Notifications:
Extend Laravel's Notification class:
class SmsNotification extends Notification {
public function via($notifiable) {
return ['kavenegar'];
}
public function toKavenegar($notifiable) {
return [
'message' => 'Your notification message',
'template' => 'notification_template',
];
}
}
API Key Security:
.env or hardcode keys in config files..env or a secure secrets manager.Rate Limits:
KavenegarException for throttling:
try {
Kavenegar::send($receiver, $message);
} catch (\Kavenegar\Exceptions\KavenegarException $e) {
if ($e->getCode() === 429) {
// Retry logic or notify admin
}
}
Template Dependencies:
verify_code) are pre-registered in Kavenegar Panel.type: 'dlr' for delivery reports, but templates must support it.Queue Stuck Jobs:
failed_jobs table for stuck SMS jobs. Implement a retry mechanism:
$job->retryAfter(5); // Retry after 5 seconds
Enable Logging:
Add to config/kavenegar.php:
'log' => true,
Check storage/logs/laravel.log for API responses/errors.
Mocking for Tests:
Use a mock service provider in config/app.php during testing:
'providers' => [
// ...
Kavenegar\Laravel\Testing\ServiceProvider::class, // Replace with mock provider
],
Custom Responses: Override the facade or service container binding:
$this->app->bind(KavenegarManager::class, function () {
return new CustomKavenegarManager();
});
Webhook Handling: Validate Kavenegar webhooks (e.g., DLR) in a route:
Route::post('/kavenegar-webhook', function (Request $request) {
$validator = Validator::make($request->all(), [
'token' => 'required|string',
'message' => 'required',
]);
// Process valid webhook
});
Fallback Mechanisms: Implement a fallback for failed sends:
try {
Kavenegar::send($receiver, $message);
} catch (Exception $e) {
Log::error("Kavenegar failed: {$e->getMessage()}");
// Fallback to email or another SMS provider
}
Line Number Validation:
Kavenegar validates sender numbers (line). Ensure your config/kavenegar.php line is registered in their panel.
Async vs Sync:
sendAsync uses a queue but may not guarantee order. Use send for critical, synchronous messages.Character Limits:
type: 'dlr' for longer messages (split internally by Kavenegar).How can I help you explore Laravel packages today?