Installation Add the bundle via Composer:
composer require cogitech/smsapi-bundle
Enable it in config/bundles.php:
return [
// ...
Cogitech\SMSAPIBundle\SMSAPIBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console smsapi:install
Update config/packages/smsapi.yaml with your SMSAPI.pl credentials:
smsapi:
api_key: 'your_api_key_here'
sender_id: 'your_sender_id'
First Use Case Send a basic SMS via a controller:
use Cogitech\SMSAPIBundle\Service\SMSAPIClient;
class MessageController extends AbstractController
{
public function sendSMS(SMSAPIClient $client)
{
$response = $client->sendSMS(
'+48123456789',
'Hello from Laravel!'
);
return new JsonResponse($response);
}
}
Sending SMS
Use the SMSAPIClient service for direct API calls:
$client->sendSMS(
$recipient, // String (e.g., '+48123456789')
$message, // String (max 160 chars)
$senderId // Optional (defaults to config)
);
Bulk SMS
For multiple recipients, use the sendBulkSMS method:
$client->sendBulkSMS(
['+48123456789', '+48987654321'],
'Your bulk message'
);
Queue Integration Wrap calls in a job for async processing:
use Cogitech\SMSAPIBundle\Job\SendSMSJob;
SendSMSJob::dispatch($recipient, $message)->onQueue('sms');
Validator:
$constraint = new PhoneNumber($options);
$errors = $validator->validate($phoneNumber, $constraint);
# config/packages/monolog.yaml
handlers:
smsapi:
type: stream
path: "%kernel.logs_dir%/smsapi.log"
level: debug
SMSSentEvent):
// src/EventListener/SMSListener.php
public function onSMSsent(SMSSentEvent $event)
{
// Handle success/failure
}
API Key Exposure
config/packages/smsapi.yaml to version control.# .env
SMSAPI_API_KEY=your_key_here
SMSAPI_SENDER_ID=your_sender
Update the config to read from .env:
smsapi:
api_key: '%env(SMSAPI_API_KEY)%'
Character Limits
$client->checkMessageLength($message); // Returns boolean
Rate Limits
use Symfony\Component\Retry\Retry;
$retry = new Retry(3, 1000); // 3 retries, 1s delay
$response = $retry->retry(
fn() => $client->sendSMS($phone, $message)
);
Sender ID Restrictions
smsapi:
sandbox: true # Uses SMSAPI.pl's test environment
smsapi:
debug: true # Logs raw API requests/responses
try-catch to handle SMSAPIException:
try {
$client->sendSMS($phone, $message);
} catch (SMSAPIException $e) {
// Handle 4xx/5xx errors (e.g., invalid API key)
$this->addFlash('error', $e->getMessage());
}
Custom Response Handling
Extend the SMSAPIClient to parse responses:
class CustomSMSAPIClient extends SMSAPIClient
{
protected function parseResponse($data)
{
// Override to add custom fields
return parent::parseResponse($data) + ['custom_field' => true];
}
}
Register as a service:
services:
App\Service\CustomSMSAPIClient:
decorates: 'smsapi.client'
Add New Endpoints
Use the HttpClient directly for unsupported API calls:
$httpClient = $this->container->get('http_client');
$response = $httpClient->request('GET', 'https://api.smsapi.pl/v1/balances');
Template Management
For reusable templates, create a SMSTemplate entity and a repository:
// src/Repository/SMSTemplateRepository.php
class SMSTemplateRepository extends ServiceEntityRepository
{
public function findByName(string $name): ?SMSTemplate
{
return $this->createQueryBuilder('t')
->where('t.name = :name')
->setParameter('name', $name)
->getQuery()
->getOneOrNullResult();
}
}
How can I help you explore Laravel packages today?