awaresoft/sonata-notification-bundle
Installation:
composer require awaresoft/sonata-notification-bundle
Ensure your config/bundles.php includes:
return [
// ...
Awaresoft\SonataNotificationBundle\AwaresoftSonataNotificationBundle::class => ['all' => true],
];
Database Migration: Run the bundle's migration to create the required tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Notification:
Use the NotificationManager service to send a basic notification:
use Awaresoft\SonataNotificationBundle\Manager\NotificationManager;
$notificationManager = $this->container->get('sonata.notification.manager');
$notificationManager->send(
'user@example.com', // Recipient
'Welcome!', // Subject
'Hello, thanks for signing up!' // Message
);
Configuration:
Override default settings in config/packages/awaresoft_sonata_notification.yaml:
awaresoft_sonata_notification:
default_from_email: 'noreply@example.com'
transport: 'mailer' # or 'swiftmailer', 'sendmail', etc.
$notificationManager->send(
'user@example.com',
'Your Order Confirmation',
'Your order #12345 has been processed.'
);
$notificationManager->sendTemplate(
'user@example.com',
'emails/order_confirmation.twig',
['order' => $order]
);
Dispatch notifications asynchronously using Symfony Messenger:
use Awaresoft\SonataNotificationBundle\Message\SendNotificationMessage;
$message = new SendNotificationMessage(
'user@example.com',
'Welcome!',
'Thanks for joining!'
);
$this->messageBus->dispatch($message);
Attach metadata (e.g., user ID, locale) for personalized delivery:
$notificationManager->send(
'user@example.com',
'Your Reminder',
'Don’t forget your appointment tomorrow!',
['user_id' => 42, 'locale' => 'en_US']
);
Send to multiple recipients efficiently:
$recipients = ['user1@example.com', 'user2@example.com'];
$notificationManager->sendBulk($recipients, 'Newsletter', 'Check this out!');
Trigger notifications on user actions (e.g., registration, order placement):
// In your event subscriber
public function onUserRegistered(UserRegisteredEvent $event)
{
$this->notificationManager->send(
$event->getUser()->getEmail(),
'Account Created',
'Your account is now active!'
);
}
Extend the bundle to support APIs (e.g., Twilio for SMS):
# config/packages/awaresoft_sonata_notification.yaml
awaresoft_sonata_notification:
transports:
sms:
class: App\Notification\Transport\TwilioTransport
options:
account_sid: 'your_sid'
auth_token: 'your_token'
Store templates in templates/emails/ and reference them:
{# templates/emails/welcome.twig #}
<h1>Welcome, {{ user.name }}!</h1>
<p>Your account was created on {{ 'now'|date('Y-m-d') }}.</p>
Mock the NotificationManager in PHPUnit:
$notificationManager = $this->createMock(NotificationManager::class);
$notificationManager->expects($this->once())
->method('send')
->with('user@example.com', 'Test', 'Message');
$this->container->set('sonata.notification.manager', $notificationManager);
Database Schema:
notification, notification_recipient). Ensure your migrations are up-to-date.php bin/console doctrine:schema:update --force if tables are missing.Transport Configuration:
swiftmailer, ensure the bundle is installed:
composer require symfony/mailer
transport in config:
awaresoft_sonata_notification:
transport: 'swiftmailer'
Caching Issues:
php bin/console cache:clear
php bin/console ca:cl
Queue Stuck Jobs:
php bin/console messenger:failed:list
php bin/console messenger:failed:remove <id>
Log Notifications:
Enable logging in config/packages/monolog.yaml:
handlers:
sonata_notification:
type: stream
path: "%kernel.logs_dir%/sonata_notification.log"
level: debug
Check Recipients: Verify recipients are correctly formatted (no typos, valid emails).
Template Errors:
emails/base.html.twig).{% extends 'emails/base.html.twig' %}
{% block body %}{% endblock %}
Custom Notification Types:
Extend the Notification entity to add fields (e.g., is_read, priority):
// src/Entity/ExtendedNotification.php
use Awaresoft\SonataNotificationBundle\Entity\Notification as BaseNotification;
class ExtendedNotification extends BaseNotification
{
private $priority;
// ...
}
Override Services:
Replace the NotificationManager with a custom class:
# config/services.yaml
services:
App\Notification\CustomNotificationManager:
decorates: 'sonata.notification.manager'
arguments: ['@.inner']
Add Transport:
Implement Awaresoft\SonataNotificationBundle\Transport\TransportInterface:
class SlackTransport implements TransportInterface
{
public function send(Notification $notification): void
{
// Send to Slack webhook
}
}
Register it in config:
awaresoft_sonata_notification:
transports:
slack:
class: App\Notification\Transport\SlackTransport
Batch Processing: For bulk notifications, use chunking to avoid memory issues:
$notificationManager->sendBulkInChunks($recipients, 100, 'Newsletter', 'Content');
Queue Workers: Scale Messenger workers for high-volume notifications:
php bin/console messenger:consume async -vv
How can I help you explore Laravel packages today?