Installation:
composer require kavenegar/laravel
Ensure Kavenegar\Laravel\ServiceProvider is added to config/app.php under providers and Kavenegar facade is added to aliases.
Publish Config:
php artisan vendor:publish --provider="Kavenegar\Laravel\ServiceProvider"
This generates config/kavenegar.php. Update with your API key and default settings (e.g., sender, template type).
First Use Case: Send a basic SMS via a facade call in a controller or service:
use Kavenegar\Facades\Kavenegar;
$result = Kavenegar::send('1234567890', '1234567890', 'Hello from Laravel!');
Sending SMS:
Kavenegar::send($receiver, $sender, $message);
Kavenegar::sendTemplate($receiver, $sender, $templateId, ['var1' => 'value1']);
dispatch(new SendKavenegarSmsJob($receiver, $sender, $message));
Verification & Validation:
$result = Kavenegar::send(...);
if ($result['status'] === 'success') {
// Handle success
}
Integration with Laravel Features:
event(new Registered($user));
// In listener:
Kavenegar::send($user->phone, '1234567890', 'Welcome!');
MustBeVerified for SMS verification:
use Kavenegar\Laravel\Notifications\VerifyPhoneNotification;
$user->notify(new VerifyPhoneNotification($token));
Configuration Management:
config(['kavenegar.sender' => 'NewSender']);
API Key Security:
.env or version control. Use Laravel’s .env file:
KAVENEGAR_API_KEY=your_key_here
config/kavenegar.php to read from .env:
'api_key' => env('KAVENEGAR_API_KEY'),
Template Limitations:
'123' not 'abc').sendTemplate().Rate Limits:
429 Too Many Requests gracefully:
try {
$result = Kavenegar::send(...);
} catch (\Kavenegar\Exceptions\KavenegarException $e) {
if ($e->getCode() === 429) {
sleep(1); // Retry after delay
}
}
Deprecation Warnings:
Enable Logging:
Add to config/kavenegar.php:
'debug' => env('APP_DEBUG', false),
Logs API responses to storage/logs/kavenegar.log.
Mocking for Tests:
Use a mock HTTP client (e.g., Guzzle) to intercept requests:
$mock = Mockery::mock('overload', Kavenegar::class);
$mock->shouldReceive('send')->andReturn(['status' => 'success']);
Common Errors:
Invalid API Key: Verify .env and config file.Template Not Found: Ensure the template ID exists in Kavenegar Panel.Sender Not Allowed: Check sender number is whitelisted in Kavenegar dashboard.Custom Responses: Extend the facade to add domain-specific logic:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class CustomKavenegar extends Facade {
protected static function getFacadeAccessor() {
return 'custom.kavenegar';
}
}
Queueable Jobs: Create a job for async sending:
namespace App\Jobs;
use Kavenegar\Facades\Kavenegar;
use Illuminate\Bus\Queueable;
class SendKavenegarSmsJob extends Job {
use Queueable;
public function handle() {
Kavenegar::send($this->receiver, $this->sender, $this->message);
}
}
Webhook Integration: Validate incoming Kavenegar webhooks (e.g., delivery reports) via middleware:
namespace App\Http\Middleware;
use Closure;
use Kavenegar\Exceptions\InvalidSignatureException;
class VerifyKavenegarWebhook {
public function handle($request, Closure $next) {
if ($request->isKavenegarWebhook()) {
$this->validateSignature($request);
}
return $next($request);
}
}
How can I help you explore Laravel packages today?