## Getting Started
### Minimal Setup
1. **Install the package** via Composer:
```bash
composer require asmitta-01/toast-bundle
config/bundles.php (if not using Flex):
return [
Asmitta\ToastBundle\AsmittaToastBundle::class => ['all' => true],
];
config/packages/asmitta_toast.yaml (default config is auto-generated):
asmitta_toast:
toast_container:
position: bottom-center
toast_item:
timer: 5000
progress_bar: false
base.html.twig):
<link href="{{ asset('bundles/asmittaToast/css/toast.css') }}" rel="stylesheet">
{{ render_toasts() }}
Trigger a flash message in a controller and render it as a toast:
// src/Controller/SomeController.php
use Asmitta\ToastBundle\Enum\ToastType;
public function successAction(): Response
{
$this->addFlash('success', 'Operation completed successfully!');
return $this->render('some_template.html.twig');
}
The toast will appear automatically when the page loads.
addFlash() with predefined types (success, error, warning, info):
$this->addFlash('success', 'User created!');
$this->addFlash('error', 'Validation failed.');
ToastType enum for explicit control:
$this->addFlash(ToastType::SUCCESS->value, 'Custom success message');
$this->addFlash('info', ['message' => 'Data saved', 'count' => 3]);
Access in Twig:
{% for message in flashbag('info') %}
{{ message.message }} ({{ message.count }} items)
{% endfor %}
@AsmittaToast/toast_items/default.html.twig) in your project:
{% extends '@AsmittaToast/toast_items/default.html.twig' %}
{% block toast_content %}
{{- parent() -}}
<div class="custom-addon">Extra content</div>
{% endblock %}
with_icon or colored_icon templates (requires Bootstrap Icons):
# config/packages/asmitta_toast.yaml
asmitta_toast:
toast_item:
template: '@AsmittaToast/toast_items/with_icon.html.twig'
Include Bootstrap Icons in your template:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
# config/packages/asmitta_toast.yaml
asmitta_toast:
toast_container:
position: top-right # Overrides default
# config/packages/asmitta_toast.yaml (dev)
asmitta_toast:
toast_item:
timer: 0 # Disable auto-hide in dev
asmitta_toast:
toast_container:
max_toasts: 3 # Max 3 toasts per type
// src/EventListener/ToastListener.php
use Asmitta\ToastBundle\Event\ToastEvent;
public function onToastRender(ToastEvent $event): void
{
$message = $event->getMessage();
if (str_contains($message, 'error')) {
$event->setMessage('Custom error: ' . $message);
}
}
Register the listener in services.yaml:
services:
App\EventListener\ToastListener:
tags:
- { name: kernel.event_listener, event: asmitta.toast.render, method: onToastRender }
Missing Assets:
assets:install is run and CSS files are included in the template./bundles/asmittaToast/css/....Flash Bag Not Clearing:
$this->get('session')->getFlashBag()->clear();
Template Overrides Not Working:
config/packages/asmitta_toast.yaml is correct and the template extends the base template properly.Bootstrap Icons Missing:
with_icon/colored_icon templates).Symfony 8 Session Issues:
v0.4.0+ (includes Symfony 8 session compatibility fixes).{% for type, messages in app.flashbag.getAll() %}
{{ dump(messages) }}
{% endfor %}
asmitta_toast.yaml is loaded:
php bin/console debug:config asmitta_toast
.asmitta-toast classes if styling is off.timer: 0 for critical messages (e.g., modals) to avoid accidental dismissal.max_toasts to prevent memory leaks from excessive flash messages (e.g., in loops).Custom Toast Types:
Extend the ToastType enum and update the config to map new types:
// src/Enum/CustomToastType.php
namespace App\Enum;
use Asmitta\ToastBundle\Enum\ToastType;
enum CustomToastType: string {
case INFO = 'info';
case NOTICE = 'notice'; // New type
}
Update the template to handle the new type.
Dynamic Positioning: Override the container position per route via a Twig function:
{% set toast_position = 'top-right' if app.request.attributes.get('_route') == 'admin_dashboard' %}
(Requires custom Twig extension to pass position to the bundle.)
AJAX Support: Render toasts dynamically via JavaScript:
// After AJAX success
$.post('/update', data, function() {
location.reload(); // Reload to render toasts (simplest approach)
// OR fetch and append toasts via API endpoint
});
max_toasts: null disables limits entirely (not 0).5000 = 5 seconds).@App/toast/custom.html.twig) for custom templates.toast class with asmitta-toast (v0.4.0 breaking change).
Update any custom CSS targeting .toast to use .asmitta-toast.
---
How can I help you explore Laravel packages today?