ariaieboy/kavenegar-laravel
Laravel integration for the Kavenegar SMS API. Install via Composer, register the service provider and facade, publish config, and set your API key. Then send messages anywhere in your app using the Kavenegar facade.
Installation
composer require ariaieboy/kavenegar-laravel
Publish the config file:
php artisan vendor:publish --provider="Ariaieboy\KavenegarLaravel\KavenegarServiceProvider"
Configuration
Edit .env with your Kavenegar API credentials:
KAVENEGAR_API_KEY=your_api_key
KAVENEGAR_API_URL=https://api.kavenegar.com/v1
First Use Case Send an SMS via a controller:
use Ariaieboy\KavenegarLaravel\Facades\Kavenegar;
public function sendSms()
{
$result = Kavenegar::send('1234567890', 'Hello from Laravel!');
return $result->success() ? 'SMS sent!' : 'Failed: ' . $result->message();
}
Where to Look First
Ariaieboy\KavenegarLaravel\Facades\Kavenegarconfig/kavenegar.phpAriaieboy\KavenegarLaravel\Exceptions\KavenegarExceptionBasic SMS Sending
Kavenegar::send($receiver, $message, ['smsType' => 'default']);
smsType, lineNumber, and token.Verification Codes
$verification = Kavenegar::sendVerificationCode($receiver, 6, [
'template' => 'verify_code',
'token' => 'user123',
]);
Batch Sending
$receivers = ['09123456789', '09987654321'];
$result = Kavenegar::sendBatch($receivers, 'Hello batch!');
Event-Based Integration Use Laravel events to trigger SMS after model actions:
// In User model
protected static function booted()
{
static::created(function ($user) {
Kavenegar::send($user->phone, "Welcome, {$user->name}!");
});
}
use Ariaieboy\KavenegarLaravel\Jobs\SendSmsJob;
SendSmsJob::dispatch($receiver, $message)->onQueue('sms');
$result = Kavenegar::send($receiver, $message);
\Log::info('Kavenegar response', $result->toArray());
try {
$result = Kavenegar::send($receiver, $message);
if (!$result->success()) {
// Fallback to email or another SMS provider
}
} catch (\Exception $e) {
\Log::error('Kavenegar error', ['error' => $e->getMessage()]);
}
API Key Exposure
.env is never committed to version control..env ignore patterns.Rate Limits
$attempts = 0;
while (!$result->success() && $attempts < 3) {
$attempts++;
sleep(2 ** $attempts); // Exponential delay
$result = Kavenegar::send($receiver, $message);
}
Receiver Validation
$receiver = preg_replace('/[^0-9]/', '', $phone); // Remove non-digits
if (!preg_match('/^09\d{9}$/', $receiver)) {
throw new \InvalidArgumentException('Invalid phone number');
}
Response Parsing
success() method returns a boolean, but the underlying response may contain additional data:
$result = Kavenegar::send($receiver, $message);
if (!$result->success()) {
\Log::error('Kavenegar error code: ' . $result->getErrorCode());
}
debug to true in config/kavenegar.php to log raw API responses:
'debug' => env('KAVENEGAR_DEBUG', false),
$response = \Http::withHeaders([
'apikey' => config('kavenegar.api_key'),
])->post(config('kavenegar.api_url') . '/sms/send.json', [
'receptor' => $receiver,
'message' => $message,
]);
Custom Templates Extend the package to support dynamic templates:
// In a service class
public function sendCustomTemplate($receiver, $data)
{
$message = $this->renderTemplate('welcome', $data);
return Kavenegar::send($receiver, $message);
}
Webhook Handling Add a route to handle Kavenegar webhook callbacks:
Route::post('/kavenegar/webhook', [KavenegarWebhookController::class, 'handle']);
KavenegarWebhookController to parse and act on delivery reports.Mocking for Tests Use Laravel's HTTP mocking to test SMS sends:
\Http::fake([
config('kavenegar.api_url') . '/*' => Http::response([
'status' => 'success',
'message' => 'Mocked SMS',
]),
]);
Multi-Provider Support Abstract the Kavenegar client to support multiple SMS providers:
// In a service provider
$this->app->bind(\Ariaieboy\KavenegarLaravel\Contracts\SmsProvider::class, function () {
return new KavenegarProvider();
// Or return new AlternativeProvider();
});
How can I help you explore Laravel packages today?