andreybolonin/sms-gateway-bundle
Since this is a Symfony bundle, direct Laravel integration requires a bridge. Use symfony/console and symfony/dependency-injection for compatibility. Start with:
Install via Composer (Symfony-compatible):
composer require andreybolonin/sms-gateway-bundle
Bootstrap Symfony Components (Laravel Service Provider):
// app/Providers/SmsGatewayServiceProvider.php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class SmsGatewayServiceProvider extends ServiceProvider {
public function register() {
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.yaml'); // Define Symfony services here
$this->app->singleton('sms.gateway', fn() => $container->get('sms.gateway'));
}
}
First Use Case: Send an SMS via Twilio
use Symfony\Component\DependencyInjection\ContainerInterface;
$gateway = app('sms.gateway');
$gateway->send('Twilio', [
'to' => '+1234567890',
'message' => 'Hello from Laravel!',
'from' => '+1987654321'
]);
Store credentials in Laravel’s .env and bind them to Symfony’s DI container:
# config/services.yaml
services:
sms.gateway:
class: AndreyBolonin\SmsGatewayBundle\Gateway\Gateway
arguments:
- '%env(TWILIO_SID)%'
- '%env(TWILIO_TOKEN)%'
- '%env(TWILIO_FROM)%'
Leverage Laravel’s retry helper for transient failures:
use Illuminate\Support\Facades\Retry;
Retry::times(3)->attempt(function () {
$gateway->send('Twilio', ['to' => '+1234567890', 'message' => 'Retry test']);
});
Wrap the gateway in a job for async processing:
// app/Jobs/SendSmsJob.php
use AndreyBolonin\SmsGatewayBundle\Gateway\Gateway;
class SendSmsJob implements ShouldQueue {
public function handle(Gateway $gateway) {
$gateway->send('Nexmo', ['to' => '+1234567890', 'message' => 'Queued SMS']);
}
}
Use Laravel’s config() to switch providers:
$provider = config('sms.default_provider');
$gateway->send($provider, ['to' => '+1234567890', 'message' => 'Dynamic provider']);
Symfony vs. Laravel DI:
ContainerBuilder expects YAML/XML config. Use symfony/dependency-injection for Laravel compatibility.SmsGatewayServiceProvider.Provider-Specific Quirks:
from number in every request.encoding: 'utf-8' explicitly.account_sid and auth_token in the same format as Symfony’s TropoGateway.Rate Limits:
throttle middleware:
Route::middleware(['throttle:60,1'])->post('/sms', [SmsController::class, 'send']);
$container->setDebug(true); // In SmsGatewayServiceProvider
$gateway->setLogger(app(\Psr\Log\LoggerInterface::class));
Custom Providers:
Extend AndreyBolonin\SmsGatewayBundle\Gateway\AbstractGateway and register via Symfony’s tags:
services:
app.custom_gateway:
class: App\Gateway\CustomGateway
tags: ['sms.gateway']
Laravel Events: Trigger events after SMS sending:
event(new SmsSent($provider, $response));
Mocking for Tests:
Use Laravel’s Mockery to stub the gateway:
$mock = Mockery::mock(AndreyBolonin\SmsGatewayBundle\Gateway\Gateway::class);
$mock->shouldReceive('send')->andReturn(['success' => true]);
How can I help you explore Laravel packages today?