michalsn/codeigniter-htmx-alerts

Alerts for working with CodeIgniter 4 framework

dev-develop 2024-10-21 05:35 UTC

This package is auto-updated.

Last update: 2024-12-21 06:03:46 UTC


README

A simple Alerts class integrated with htmx and Alpine.js for CodeIgniter 4 framework.

PHPUnit PHPStan Deptrac Coverage Status

PHP CodeIgniter

Installation

composer require michalsn/codeigniter-htmx-alerts

Configuration

You have to publish the config file first:

php spark alerts:publish

Now you can change:

$key

Alerts key name used in views and session.

$displayTime

The default alert display time in milliseconds.

$types

The array of message types, where array key is a CSS class and value is the title of the alert type.

Array keys are also used to determine the type of the alert we want to set, ie:

alerts()->set('success', 'Success message goes here.');

$htmlWrapperId

Wrapper id name, used in the view file.

$views

View files used by this library. You can change them to reflect the current style/theme you're using.

The default view files are designed to play along with Tabler theme.

Usage

In your main layout place the code (usually it will be just before the closing </body> tag):

<?= alerts()->container(); ?>

That's it. You're ready to go. No matter if this is a htmx request or traditional one, your alerts will be placed correctly every time.

Adding alerts

You can add alerts in your controllers.

// success alert
alerts()->set('success', 'Success message');
// error message
alerts()->set('danger', 'Error message');
// custom display time - 1 sec (in milliseconds)
alerts()->set('success', 'Message', 1000);

Removing alerts

You can also remove alerts by type.

// will remove all success alerts
alerts()->clear('success');
// will remove all alerts
alerts()->clear();