Installation
composer require coderspotting/toast-message
(Note: Due to the package being archived, verify compatibility with your Symfony/Laravel version if using outside Symfony.)
Include Assets in Twig (Symfony) or Blade (Laravel via Bridge)
Add these snippets to your base template (e.g., base.blade.php or base.html.twig):
{% stylesheets filter='cssrewrite' 'bundles/coderspottingtoastmessage/css/*' %}
<link href="{{ asset_url }}" rel="stylesheet" media="screen" />
{% endstylesheets %}
{% javascripts '@CoderSpottingToastMessageBundle/Resources/public/js/*' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
(For Laravel, manually include the CSS/JS from vendor/coderspotting/toast-message/Resources/public/.)
Render Toasts Place this after the JS inclusion:
{{ renderToasts() }}
First Use Case Trigger a toast from a controller:
$toast = $this->get('CoderSpotting.ToastMessage')->addToast(
'Success!',
'Your action was completed.',
'success'
);
Service Integration
CoderSpotting\ToastMessageBundle\Service\ToastMessage or fetch via container:
$toastService = $this->get('CoderSpotting.ToastMessage');
$toast = new \CoderSpotting\ToastMessageBundle\Service\ToastMessage();
Toast Types
Use Alertify.js’s built-in types (success, error, warning, info, notice). Extend via custom CSS classes:
$toastService->addToast('Title', 'Message', 'custom-class', ['data-attr' => 'value']);
Queueing Toasts Toasts persist until rendered. Clear them after display:
$toastService->clearToasts(); // Call post-render (e.g., in a Twig extension).
Dynamic Content Pass variables to Twig for dynamic rendering:
$toastService->addToast('{{ user.name }}', 'Welcome back!', 'success');
Service Provider Setup
Register the service in AppServiceProvider:
public function register()
{
$this->app->singleton('toast', function ($app) {
return new \CoderSpotting\ToastMessageBundle\Service\ToastMessage();
});
}
Helper Methods Create facade or helper for brevity:
// In a helper file (e.g., `app/Helpers/ToastHelper.php`)
function toast($title, $message, $type = 'success')
{
app('toast')->addToast($title, $message, $type);
}
Blade Directives Extend Blade to auto-render toasts:
// In AppServiceProvider@boot()
Blade::directive('toasts', function () {
return "<?php echo app('toast')->renderToasts(); ?>";
});
Usage in Blade:
@toasts
Archived Package
Asset Loading
asset() may fail if assets:install isn’t run.mix.copy():
// webpack.mix.js
mix.copy('vendor/coderspotting/toast-message/Resources/public', 'public/vendor/toast-message');
Toast Persistence
// In a Twig extension or controller post-action
$toastService->clearToasts();
JavaScript Conflicts
renderToasts(). Use defer if needed:
<script src="{{ asset_url }}" defer></script>
No Toasts Displaying?
renderToasts() is called after JS inclusion.Styling Issues
.alertify-notification)..alertify-notification.success {
background: #4CAF50 !important;
}
Symfony-Specific
php bin/console cache:clear
Custom Toast Templates
Override Alertify’s templates by extending its templates config:
// In your JS file (loaded after Alertify)
alertify.defaults.notification.template = `
<div class="custom-toast">
<h2>{1}</h2>
<p>{2}</p>
</div>
`;
Laravel Notifications
Integrate with Laravel’s notification system (e.g., trigger toasts from Notifiable):
use Illuminate\Notifications\Notifiable;
class User extends Model implements Notifiable
{
public function sendToast($message)
{
app('toast')->addToast('Notification', $message, 'info');
}
}
Ajax Requests For SPAs, manually trigger toasts via JS:
// In your JS file
window.addToast = function(title, message, type) {
alertify.alert(title, message, type);
};
Call from Laravel:
echo "<script>window.addToast('{$title}', '{$message}', '{$type}');</script>";
How can I help you explore Laravel packages today?