Installation
composer require dywee/notification-bundle
Add to AppKernel.php (Symfony3):
new Dywee\NotificationBundle\DyweeNotificationBundle(),
First Use Case Trigger a notification for a user:
use Dywee\NotificationBundle\Manager\NotificationManager;
$notificationManager = $this->get('dywee_notification.manager');
$notificationManager->createNotification(
$userId, // Target user ID
'system', // Channel (e.g., 'system', 'email', 'push')
'welcome', // Notification type
['name' => 'John'] // Data payload
);
Where to Look First
config.yml (if provided) or Resources/config/services.yml for bundle settings.NotificationManager for core functionality.Resources/views/ for default notification templates (if any).Notification Creation
Use NotificationManager to dispatch notifications:
$manager->createNotification(
$userId,
'email', // Channel
'order_confirmation',
['order_id' => 123]
);
Channel-Specific Logic
Extend or override channel handlers (e.g., EmailChannel, PushChannel) in Resources/config/notification_channels.yml:
dywee_notification:
channels:
email:
class: AppBundle\Notification\Channel\CustomEmailChannel
Event Listeners
Attach listeners to notification.created or notification.sent events:
// In a service or controller
$this->get('event_dispatcher')->dispatch(
'notification.created',
new NotificationEvent($notification)
);
Template Overrides Override default templates by copying them from the bundle to your theme:
src/AppBundle/Resources/views/Notifications/
postPersist/postUpdate to auto-generate notifications (e.g., for new comments).$manager->sendNotificationAsync($userId, 'email', 'newsletter', $data);
Channel Configuration
notification_channels.yml will silently fail.dywee_notification:
channels:
email:
enabled: true
class: Dywee\NotificationBundle\Channel\EmailChannel
User Context
$userId is invalid or the user object isn’t loaded.UserProviderInterface to resolve users:
$user = $this->get('security.token_storage')->getToken()->getUser();
$manager->createNotification($user->getId(), ...);
Template Inheritance
{% extends 'DyweeNotificationBundle::base.html.twig' %}.Debugging
dywee_notification.logger for sent notifications:
$this->get('dywee_notification.logger')->debug('Notification sent', ['data' => $data]);
Custom Channels
Implement Dywee\NotificationBundle\Channel\ChannelInterface:
class SlackChannel implements ChannelInterface {
public function send(Notification $notification) {
// Slack API logic
}
}
Notification Types
Extend the Notification entity or use tags:
$manager->createNotification($userId, 'email', 'promotion', $data, ['tag' => 'marketing']);
Validation
Add validation to NotificationManager via a subscriber:
$event->setNotification($event->getNotification()->withData(['validated' => true]));
Testing
Mock NotificationManager in tests:
$manager = $this->createMock(NotificationManager::class);
$manager->expects($this->once())->method('createNotification');
$this->container->set('dywee_notification.manager', $manager);
How can I help you explore Laravel packages today?