Installation:
composer require ehann/notification-bundle:1.*
Add to config/bundles.php (Symfony 4+):
Ehann\NotificationBundle\EhannNotificationBundle::class => ['all' => true],
First Use Case: Trigger a notification in a controller:
use Ehann\NotificationBundle\Notification\NotificationManager;
public function someAction(NotificationManager $notificationManager)
{
$notificationManager->addSuccess('Operation completed!');
// or: addError(), addWarning(), addInfo()
}
Where to Look First:
src/Ehann/NotificationBundle/Resources/views/Notification/notification.html.twig (default template)config/packages/ehann_notification.yaml (if it exists; otherwise, check for bundle-specific config).Triggering Notifications:
Inject NotificationManager into controllers/services and use methods like:
$notificationManager->addSuccess('Message', ['key' => 'value']);
$notificationManager->addError('Error occurred!', [], 'custom_id');
Customizing Appearance:
Override the Twig template (notification.html.twig) in your theme:
{# Example: Extend with icons #}
{% extends 'EhannNotificationBundle::Notification/notification.html.twig' %}
{% block icon %}
{% if type == 'success' %}
<i class="fas fa-check-circle"></i>
{% endif %}
{% endblock %}
Flash vs. Persistent Notifications:
{% for notification in app.notifications %}
{% include 'EhannNotificationBundle::Notification/notification.html.twig' %}
{% endfor %}
Integration with Forms: Validate form submissions and flash errors:
if (!$form->isValid()) {
foreach ($form->getErrors(true) as $error) {
$notificationManager->addError($error->getMessage());
}
}
Dependency Injection: Extend the bundle’s services (e.g., add a custom notification type):
# config/services.yaml
services:
App\Notification\CustomNotifier:
arguments:
- '@ehann_notification.notification_manager'
Symfony 4+ Compatibility:
NotificationManagerInterface for type-hinting and configure autowiring in config/services.yaml:
Ehann\NotificationBundle\Notification\NotificationManagerInterface: '@ehann_notification.notification_manager'
Template Overrides:
templates/bundles/EhannNotification/Notification/notification.html.twig).php bin/console cache:clear) after template changes.Session Dependencies:
Type-Specific Styling:
success, error, warning, and info types. Custom types (e.g., alert) require CSS classes in the template:
{% if type == 'alert' %}
<div class="alert alert-danger">...</div>
{% endif %}
Legacy Symfony 2 Features:
FrameworkBundle is loaded for compatibility.Reusable Notification Components: Create a base controller to avoid repetition:
abstract class BaseController extends AbstractController
{
protected NotificationManager $notificationManager;
public function __construct(NotificationManager $notificationManager)
{
$this->notificationManager = $notificationManager;
}
protected function flashSuccess(string $message): void
{
$this->notificationManager->addSuccess($message);
}
}
Localization: Translate messages in your templates:
{{ 'notification.success'|trans({ 'message': message }, 'messages') }}
Testing:
Mock NotificationManager in PHPUnit:
$notificationManager = $this->createMock(NotificationManagerInterface::class);
$notificationManager->expects($this->once())
->method('addSuccess')
->with('Expected message');
Extending Notification Types: Add a custom type by extending the manager:
class ExtendedNotificationManager extends NotificationManager
{
public function addAlert(string $message, array $parameters = [], string $key = null): void
{
$this->addNotification('alert', $message, $parameters, $key);
}
}
Register it as a service:
services:
App\Notification\ExtendedNotificationManager: '@ehann_notification.notification_manager'
Performance: For high-traffic apps, avoid storing large notification data in the session. Use short messages or IDs to reference data elsewhere.
How can I help you explore Laravel packages today?