Installation Add the bundle via Composer (if still functional):
composer require byscripts/alert-bundle
Register in AppKernel.php (Symfony 2.x):
new Byscripts\AlertBundle\ByscriptsAlertBundle(),
First Use Case Trigger a basic alert in a controller:
use Byscripts\AlertBundle\AlertManager;
class TestController extends Controller
{
public function showAlertAction()
{
$alertManager = $this->get('byscripts_alert.manager');
$alertManager->addAlert('success', 'Operation completed!');
return $this->render('template.html.twig');
}
}
Twig Integration
Check templates/ByscriptsAlertBundle/alerts.html.twig for default styling. Override in your theme if needed.
Alert Types
Use predefined types (success, error, warning, info) or extend:
$alertManager->addAlert('custom', 'Message', ['icon' => 'fa-exclamation']);
Flash vs. Session Alerts
$alertManager->addFlashAlert('error', 'Temporary error');
$alertManager->addAlert('warning', 'Persistent warning');
Contextual Alerts Attach metadata (e.g., for AJAX responses):
$alertManager->addAlert('info', 'Data saved', ['ajax' => true]);
Event-Driven Alerts Listen for events (e.g., form submission) and trigger alerts:
$dispatcher->addListener('form.submit', function() use ($alertManager) {
$alertManager->addAlert('success', 'Form submitted!');
});
Custom Templates
Override alerts.html.twig to modify rendering:
{% block byscripts_alerts %}
<div class="custom-alert">
{% for alert in alerts %}
<div class="alert alert-{{ alert.type }}">{{ alert.message }}</div>
{% endfor %}
</div>
{% endblock %}
API Responses Serialize alerts for JSON responses:
return new JsonResponse([
'success' => true,
'alerts' => $alertManager->getAlerts()
]);
Deprecated Bundle
Session Dependency
Alerts rely on the session handler. Ensure session.driver is configured in .env:
SESSION_DRIVER=file
No Laravel Support Requires Symfony 2.x. For Laravel, consider alternatives like:
laravel-notification-channels/alertLimited Documentation Assume undocumented behavior (e.g., alert storage duration, max alerts per request).
Alert Not Showing? Check if the Twig block is included in your layout:
{% block byscripts_alerts %}{% endblock %}
Clear cache if overrides aren’t applying:
php app/console cache:clear
Session Issues Verify session is started before adding alerts:
$request->getSession()->start();
Custom Alert Types
Extend the Alert class or use a service decorator:
$alertManager->addAlertType('confirm', function($message) {
return ['type' => 'confirm', 'message' => $message, 'icon' => 'fa-check-circle'];
});
Storage Backend
Override the session storage by implementing Byscripts\AlertBundle\Storage\AlertStorageInterface.
JavaScript Integration
Use the ajax flag to trigger JS alerts:
{% if alert.ajax %}
<script>alert('{{ alert.message }}');</script>
{% endif %}
How can I help you explore Laravel packages today?