Installation
composer require atakajlo/notification-bundle
Add the bundle to config/bundles.php (Symfony):
return [
// ...
Atakajlo\NotificationBundle\AtakajloNotificationBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console atakajlo:notification:install
Edit config/packages/atakajlo_notification.yaml to define channels (e.g., email, centrifugal, database).
First Notification
Create a notification class (e.g., src/Notification/WelcomeNotification.php):
namespace App\Notification;
use Atakajlo\NotificationBundle\Notification\NotificationInterface;
class WelcomeNotification implements NotificationInterface
{
public function getChannel(): string
{
return 'email'; // Matches config key
}
public function getData(): array
{
return [
'title' => 'Welcome!',
'message' => 'Thanks for signing up.',
];
}
}
Trigger a Notification
use Atakajlo\NotificationBundle\Notification\NotificationManager;
$notificationManager = $container->get(NotificationManager::class);
$notificationManager->send(new WelcomeNotification(), $userId);
config/packages/atakajlo_notification.yaml (channels, drivers, Centrifugo settings).php bin/console list atakajlo (e.g., atakajlo:notification:install, atakajlo:notification:send-test).NotificationInterface in src/Notification/.templates/AtakajloNotificationBundle/ (if using email/HTML).centrifugal channel in config/packages/atakajlo_notification.yaml:
channels:
centrifugal:
driver: centrifugal
options:
url: "http://centrifugo:4000/connection/websocket"
token: "your-secret-token"
CentrifugalNotification class:
class CentrifugalNotification implements NotificationInterface
{
public function getChannel(): string { return 'centrifugal'; }
public function getData(): array { return ['event' => 'new_message', 'data' => [...]]; }
}
php bin/console atakajlo:notification:send-test App\Notification\CentrifugalNotification 123
NotificationInterface for each.# config/packages/atakajlo_notification.yaml
channels:
email:
driver: mail
options:
from: "no-reply@example.com"
sms:
driver: twilio
options:
sid: "your_sid"
token: "your_token"
class OrderConfirmationNotification implements NotificationInterface
{
public function getChannel(): string { return 'email'; }
public function getData(): array { return ['order_id' => 123]; }
}
getChannel() to route based on user preferences or context:
public function getChannel(): string
{
return $this->user->prefersEmail() ? 'email' : 'centrifugal';
}
send-batch command for bulk notifications:
php bin/console atakajlo:notification:send-batch App\Notification\WelcomeNotification 1,2,3,4,5
user.registered):
// src/EventListener/UserRegisteredListener.php
public function onUserRegistered(UserRegisteredEvent $event)
{
$notificationManager->send(new WelcomeNotification(), $event->getUser()->getId());
}
Service Container
Bind the NotificationManager in config/services.php:
'notification_manager' => Atakajlo\NotificationBundle\Notification\NotificationManager::class,
Access via:
$this->container->get('notification_manager');
Queue Integration
Configure the queue driver in config/packages/atakajlo_notification.yaml:
channels:
queue:
driver: queue
options:
connection: database
Then dispatch notifications to queues:
$notificationManager->sendLater(new WelcomeNotification(), $userId, now()->addMinutes(5));
Custom Drivers
Extend Atakajlo\NotificationBundle\Driver\DriverInterface for new channels (e.g., Slack, Push):
class SlackDriver implements DriverInterface
{
public function send(NotificationInterface $notification, $recipient): void
{
// Logic to send via Slack Webhook
}
}
Register in config:
channels:
slack:
driver: slack
options:
webhook_url: "https://hooks.slack.com/..."
Twig Integration
Override email templates in templates/AtakajloNotificationBundle/email/:
{# templates/AtakajloNotificationBundle/email/welcome.html.twig #}
<h1>{{ notification.data.title }}</h1>
<p>{{ notification.data.message }}</p>
Channel Mismatch
NotificationInterface::getChannel() returns a key that doesn’t exist in config.config/packages/atakajlo_notification.yaml keys.Channel [X] not configured errors.Centrifugo Connection Issues
phpcent is installed and Centrifugo server is running.options.url and options.token in config.config/packages/atakajlo_notification.yaml:
debug: true
Proprietary License
proprietary (unusual for open-source).LICENSE file or contact maintainer for clarification before production use.Queue Stuck Jobs
database or redis queue connection is configured in .env.php bin/console messenger:consume async (if using Symfony Messenger).Enable Debug Mode
# config/packages/atakajlo_notification.yaml
debug: true
Logs detailed events to var/log/dev.log.
Test Commands
php bin/console atakajlo:notification:send-test App\Notification\WelcomeNotification 1
php bin/console atakajlo:notification:list
Driver-Specific Logs
php bin/console debug:mailer:messages
wscat.Environment-Specific Config
Override config per environment (e.g., config/packages/dev/atakajlo_notification.yaml):
debug: true
channels:
centrifugal:
options:
url: "ws://localhost:4000/connection/websocket"
Notification Metadata Add metadata to track notifications (e.g., sent_at, status):
public function getMetadata(): array
{
return [
'priority' => 'high',
'template' => 'custom_template',
];
}
Rate Limiting
Use Symfony’s RateLimiter to throttle notifications:
# config/packages/atakajlo_notification.yaml
rate_limiter:
enabled: true
limit: 10
interval: 60
Custom Recipient Mapping
Override recipient resolution in NotificationManager:
How can I help you explore Laravel packages today?