Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Toast Message Laravel Package

coderspotting/toast-message

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require coderspotting/toast-message
    

    (Note: Due to the package being archived, verify compatibility with your Symfony/Laravel version if using outside Symfony.)

  2. 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/.)

  3. Render Toasts Place this after the JS inclusion:

    {{ renderToasts() }}
    
  4. First Use Case Trigger a toast from a controller:

    $toast = $this->get('CoderSpotting.ToastMessage')->addToast(
        'Success!',
        'Your action was completed.',
        'success'
    );
    

Implementation Patterns

Core Workflows

  1. Service Integration

    • Symfony: Autowire CoderSpotting\ToastMessageBundle\Service\ToastMessage or fetch via container:
      $toastService = $this->get('CoderSpotting.ToastMessage');
      
    • Laravel: Use a service provider to bind the service (example below) or manually instantiate:
      $toast = new \CoderSpotting\ToastMessageBundle\Service\ToastMessage();
      
  2. 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']);
    
  3. Queueing Toasts Toasts persist until rendered. Clear them after display:

    $toastService->clearToasts(); // Call post-render (e.g., in a Twig extension).
    
  4. Dynamic Content Pass variables to Twig for dynamic rendering:

    $toastService->addToast('{{ user.name }}', 'Welcome back!', 'success');
    

Laravel-Specific Patterns

  1. Service Provider Setup Register the service in AppServiceProvider:

    public function register()
    {
        $this->app->singleton('toast', function ($app) {
            return new \CoderSpotting\ToastMessageBundle\Service\ToastMessage();
        });
    }
    
  2. 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);
    }
    
  3. 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
    

Gotchas and Tips

Pitfalls

  1. Archived Package

    • No active maintenance; test thoroughly. Fork if critical for your project.
    • Alertify.js v1.x may have vulnerabilities (upgrade to v2+ if possible).
  2. Asset Loading

    • Symfony: Twig’s asset() may fail if assets:install isn’t run.
    • Laravel: Manually publish assets or use mix.copy():
      // webpack.mix.js
      mix.copy('vendor/coderspotting/toast-message/Resources/public', 'public/vendor/toast-message');
      
  3. Toast Persistence

    • Toasts render once per request cycle. Clear them post-render to avoid duplicates:
      // In a Twig extension or controller post-action
      $toastService->clearToasts();
      
  4. JavaScript Conflicts

    • Ensure Alertify.js is loaded before renderToasts(). Use defer if needed:
      <script src="{{ asset_url }}" defer></script>
      

Debugging

  1. No Toasts Displaying?

    • Check browser console for JS errors (e.g., missing Alertify).
    • Verify renderToasts() is called after JS inclusion.
  2. Styling Issues

    • Override CSS via custom stylesheet (Alertify’s classes: .alertify-notification).
    • Example:
      .alertify-notification.success {
          background: #4CAF50 !important;
      }
      
  3. Symfony-Specific

    • Clear cache after installation:
      php bin/console cache:clear
      

Extension Points

  1. 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>
    `;
    
  2. 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');
        }
    }
    
  3. 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>";
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity