Installation
composer require blackford/twilio-bundle
Environment Setup
Add Twilio credentials to .env.local:
TWILIO_USER=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_PASSWORD=your_auth_token_or_password
Configuration
Create config/packages/twilio.yml with:
blackford_twilio:
username: '%env(TWILIO_USER)%'
password: '%env(TWILIO_PASSWORD)%'
First Use Case
Inject the TwilioClient service into a controller/service and use it like the official SDK:
use Blackford\TwilioBundle\Client\TwilioClient;
class MyController extends AbstractController
{
public function sendSms(TwilioClient $twilio): Response
{
$message = $twilio->messages->create(
'+1234567890', // To
'+0987654321', // From
'Hello from Symfony!'
);
return new Response('SMS sent: ' . $message->sid);
}
}
Sending SMS
$twilio->messages->create($to, $from, $body, [
'statusCallback' => $this->generateUrl('callback_route'),
]);
Making Calls
$call = $twilio->calls->create(
$from,
$to,
$this->generateUrl('call_status_route')
);
Handling Webhooks
Use Symfony’s Request and EventDispatcher to validate and process Twilio webhooks:
use Blackford\TwilioBundle\Event\TwilioWebhookEvent;
public function onTwilioWebhook(TwilioWebhookEvent $event)
{
if ($event->isValid()) {
$data = $event->getData();
// Process SMS/call status updates
}
}
Service Integration
Bind the TwilioClient to a dedicated service for reusability:
# config/services.yaml
services:
App\Service\TwilioService:
arguments:
$twilio: '@blackford_twilio.client'
.env.local for credentials (never hardcode).TwilioClient over instantiating it manually.Twilio\Rest\ApiException:
try {
$twilio->messages->create(...);
} catch (ApiException $e) {
$this->addFlash('error', $e->getMessage());
}
Deprecated SDK Version The bundle wraps Twilio SDK v6, but the last release was in 2016. Verify compatibility with the latest SDK if using newer Twilio features.
Configuration Overrides
The bundle does not support dynamic configuration via config/packages/twilio.yaml overrides in Symfony 5.4+. Use environment variables or parameters.yaml instead:
# config/packages/parameters.yaml
parameters:
twilio.username: '%env(TWILIO_USER)%'
twilio.password: '%env(TWILIO_PASSWORD)%'
Webhook Validation
Twilio webhooks require strict validation. Use the bundle’s TwilioWebhookEvent or manually validate signatures:
$event->isValid($request->getContent(), $request->headers->get('X-Twilio-Signature'));
Rate Limiting
Twilio enforces rate limits. Cache TwilioClient instances to avoid reconnection overhead:
# config/services.yaml
services:
blackford_twilio.client:
public: true
synthetic: true
TWILIO_DEBUG=true in .env.local to log requests/responses.dd($request->headers->all()) to inspect webhook payloads.monolog:
$twilio->setLogger(new MonologLogger($this->container->get('logger')));
Custom Services
Extend the bundle by creating a decorator for TwilioClient:
class CustomTwilioClient extends TwilioClient
{
public function sendTransactionalSms($to, $templateId, $data)
{
return $this->messages->create($to, $this->getFromNumber(), $this->renderTemplate($templateId, $data));
}
}
Event Subscribers Listen to Twilio events globally:
class TwilioSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
TwilioEvents::WEBHOOK => 'onWebhook',
];
}
}
Messenger Integration Dispatch Twilio operations as async messages:
$this->messageBus->dispatch(new SendSmsMessage($to, $body));
How can I help you explore Laravel packages today?