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

Alertify Bundle Laravel Package

appventus/alertify-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation: Replace the package in composer.json with the maintained fork:

    composer require troopers/alertify-bundle
    

    Update AppKernel.php (Symfony 2.x) or config/bundles.php (Symfony 3/4/5) to include:

    new Troopers\AlertifyBundle\TroopersAlertifyBundle(),
    
  2. Basic Usage: Inject the Alertify service in a controller:

    use Troopers\AlertifyBundle\Service\Alertify;
    
    class DemoController extends Controller
    {
        public function showAlert(Alertify $alertify)
        {
            $alertify->success('This is a success message!');
            return $this->render('demo/index.html.twig');
        }
    }
    
  3. First Template Integration: Ensure alertify.js and alertify.min.css are included in your base template (check Resources/public/ for assets). Example Twig:

    {{ parent() }}
    {% block javascripts %}
        {{ parent() }}
        <script src="{{ asset('bundles/troopersalertify/js/alertify.min.js') }}"></script>
        <script src="{{ asset('bundles/troopersalertify/js/alertify.default.css') }}"></script>
    {% endblock %}
    

Implementation Patterns

Common Workflows

  1. Conditional Alerts: Use ternary logic or helper methods to trigger alerts dynamically:

    $alertify->{(success|error|warning|message)($message, $title = null, $options = [])};
    

    Example:

    if ($user->save()) {
        $alertify->success('User saved!', 'Success', ['delay' => 5]);
    } else {
        $alertify->error('Validation failed.', 'Error', ['label' => 'Oops']);
    }
    
  2. Flash Messages: Combine with Symfony’s flash bag for persistent alerts:

    $this->get('session')->getFlashBag()->add('alertify', [
        'type' => 'success',
        'message' => 'Action completed!',
        'title' => 'Done'
    ]);
    

    Render in Twig:

    {% for type, messages in app.flashes('alertify') %}
        {% for message in messages %}
            {{ alertify[type](message.title, message.message) }}
        {% endfor %}
    {% endfor %}
    
  3. Ajax Responses: Return JSON with alert data for AJAX calls:

    return new JsonResponse([
        'success' => true,
        'alert' => [
            'type' => 'success',
            'message' => 'Data saved',
        ]
    ]);
    

    Client-side (JavaScript):

    if (response.alert) {
        alertify[response.alert.type](response.alert.message);
    }
    
  4. Custom Templates: Override default templates by copying files from: vendor/troopers/alertify-bundle/Resources/views/ to: app/Resources/TroopersAlertifyBundle/views/.


Integration Tips

  • Asset Management: Use Symfony’s asset pipeline (%kernel.root_dir%/../web/bundles/troopersalertify/) for production builds. For Webpack Encore, alias the bundle’s JS/CSS paths in webpack.config.js:

    resolve: {
        alias: {
            'alertify': path.resolve(__dirname, '../vendor/troopers/alertify-bundle/Resources/public/js/alertify.min.js'),
        }
    }
    
  • Translation: Extend the bundle’s translation files (Resources/translations/) or use Symfony’s translation system:

    {{ alertify.success(app.trans('messages.saved')) }}
    
  • Event Listeners: Trigger alerts on events (e.g., kernel.response):

    # config/services.yaml
    services:
        App\EventListener\AlertifyListener:
            tags:
                - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
    
    public function onKernelResponse(GetResponseEvent $event)
    {
        $response = $event->getResponse();
        if ($response instanceof JsonResponse && $response->isSuccessful()) {
            $this->alertify->success('Operation successful');
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • The original appventus/alertify-bundle is archived. Always use troopers/alertify-bundle for updates.
    • Check for breaking changes in the Troopers fork’s changelog.
  2. Asset Paths:

    • Hardcoding paths like bundles/appventusalertify/ will fail. Use asset() or path() helpers:
      {{ asset('bundles/troopersalertify/js/alertify.min.js') }}
      
  3. JavaScript Conflicts:

    • Alertify may conflict with other libraries (e.g., jQuery UI). Initialize it after DOM ready:
      document.addEventListener('DOMContentLoaded', function() {
          alertify.defaultSetting = { /* custom settings */ };
      });
      
  4. Symfony 5+ Compatibility:

    • The bundle lacks explicit Symfony 5+ support. Test with:
      composer require symfony/framework-bundle:^5.0 --dev
      
    • If issues arise, patch composer.json to require symfony/framework-bundle:^4.4|^5.0.
  5. Flash Bag Serialization: Custom flash messages must implement JsonSerializable:

    class AlertifyFlash implements JsonSerializable
    {
        public function jsonSerialize() { return ['type' => $this->type, 'message' => $this->message]; }
    }
    

Debugging Tips

  1. Check Initialization: Verify Alertify is loaded by inspecting the console for alertify object:

    console.log(typeof alertify); // Should log "object"
    
  2. Log Alertify Calls: Override the service temporarily to debug:

    # config/services_test.yaml
    services:
        Troopers\AlertifyBundle\Service\Alertify:
            class: Troopers\AlertifyBundle\Service\Alertify
            calls:
                - [setContainer, ['@service_container']]
            config:
                logger: '@logger'
    
  3. CSS Overrides: Use browser dev tools to inspect .alertify classes. Reset styles if needed:

    .alertify {
        z-index: 9999 !important; /* Ensure alerts appear above modals */
    }
    

Extension Points

  1. Custom Alert Types: Extend the Alertify service to add methods:

    // src/Service/ExtendedAlertify.php
    class ExtendedAlertify extends \Troopers\AlertifyBundle\Service\Alertify
    {
        public function info($message, $title = null, array $options = [])
        {
            return $this->message($message, $title, array_merge(['label' => 'Info'], $options));
        }
    }
    

    Register as a replacement service:

    services:
        Troopers\AlertifyBundle\Service\Alertify:
            alias: App\Service\ExtendedAlertify
    
  2. Dynamic Theming: Use Twig to conditionally load themes:

    {% if app.environment == 'prod' %}
        <link rel="stylesheet" href="{{ asset('bundles/troopersalertify/css/alertify.min.css') }}">
    {% else %}
        <link rel="stylesheet" href="{{ asset('bundles/troopersalertify/css/alertify.default.css') }}">
    {% endif %}
    
  3. Webpack Integration: For SPAs, import Alertify directly in JavaScript:

    import alertify from 'alertifyjs';
    alertify.defaultSetting.theme.ok = 'alertify-theme-ok';
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware