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(),
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');
}
}
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 %}
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']);
}
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 %}
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);
}
Custom Templates:
Override default templates by copying files from:
vendor/troopers/alertify-bundle/Resources/views/ to:
app/Resources/TroopersAlertifyBundle/views/.
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');
}
}
Deprecated Package:
appventus/alertify-bundle is archived. Always use troopers/alertify-bundle for updates.Asset Paths:
bundles/appventusalertify/ will fail. Use asset() or path() helpers:
{{ asset('bundles/troopersalertify/js/alertify.min.js') }}
JavaScript Conflicts:
document.addEventListener('DOMContentLoaded', function() {
alertify.defaultSetting = { /* custom settings */ };
});
Symfony 5+ Compatibility:
composer require symfony/framework-bundle:^5.0 --dev
composer.json to require symfony/framework-bundle:^4.4|^5.0.Flash Bag Serialization:
Custom flash messages must implement JsonSerializable:
class AlertifyFlash implements JsonSerializable
{
public function jsonSerialize() { return ['type' => $this->type, 'message' => $this->message]; }
}
Check Initialization:
Verify Alertify is loaded by inspecting the console for alertify object:
console.log(typeof alertify); // Should log "object"
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'
CSS Overrides:
Use browser dev tools to inspect .alertify classes. Reset styles if needed:
.alertify {
z-index: 9999 !important; /* Ensure alerts appear above modals */
}
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
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 %}
Webpack Integration: For SPAs, import Alertify directly in JavaScript:
import alertify from 'alertifyjs';
alertify.defaultSetting.theme.ok = 'alertify-theme-ok';
How can I help you explore Laravel packages today?