PHPackages                             rafaelwendel/ci4alerts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. rafaelwendel/ci4alerts

ActiveLibrary[Queues &amp; Workers](/categories/queues)

rafaelwendel/ci4alerts
======================

A helper to display message alerts in CodeIgniter 4 framework.

0.0.1(1mo ago)01MITPHPPHP ^8.0

Since Jul 3Pushed 1mo agoCompare

[ Source](https://github.com/rafaelwendel/CI4Alerts)[ Packagist](https://packagist.org/packages/rafaelwendel/ci4alerts)[ Docs](https://github.com/rafaelwendel/CI4Alerts)[ RSS](/packages/rafaelwendel-ci4alerts/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

CI4Alerts
=========

[](#ci4alerts)

**CI4Alerts** is a helper library for the **CodeIgniter 4** framework designed to simplify the creation, management, and display of message alerts (such as success, error, and warning messages) using the most popular CSS frameworks: **Bulma**, **Bootstrap**, **Materialize**, and **Tailwind CSS**.

---

Installation
------------

[](#installation)

The package can be easily installed via **Composer**. Run the following command in your terminal at the root of your CodeIgniter 4 project:

```
composer require rafaelwendel/ci4alerts
```

---

Configuration
-------------

[](#configuration)

### Setup Command (Spark)

[](#setup-command-spark)

After installing the library, you should publish the package's default configuration template file to your application's Config directory (`app/Config`). To do this, run the Spark command below in your terminal:

```
php spark alerts:setup
```

This command will copy the configuration template file to `app/Config/Alerts.php`.

---

### Configuration Options

[](#configuration-options)

When you open `app/Config/Alerts.php`, you will find the following configuration options:

- **`$active`**: Defines the default styling library that will be used by the service. The supported options are:
    - `'bulma'` (Default)
    - `'bootstrap'`
    - `'materialize'`
    - `'tailwind'`
- **`$config`**: Contains framework-specific styling and behavior sub-arrays for each supported CSS framework. The options inside each framework sub-array are:
    - `classSucess`: CSS class applied to success alerts.
    - `classError`: CSS class applied to error/danger alerts.
    - `classWarning`: CSS class applied to attention/warning alerts.
    - `sessionMsg`: Session key name (flashdata) used to store the message text.
    - `sessionMsgType`: Session key name (flashdata) used to store the message type.
    - `template`: HTML template where the CSS class and message will be injected. Supports PHP formatting placeholders (`%s`) or replacement tags (`{class}` and `{message}`).

### Adding Custom Configurations

[](#adding-custom-configurations)

If you do not want to use any of the built-in libraries, you can easily define your own custom configurations by adding new keys to the `$config` array. To use a custom configuration, simply set the `$active` property to your custom key:

```
public $active = 'my_custom_theme';

public $config = [
    // ...
    'my_custom_theme' => [
        'classSucess'    => 'my-custom-success-class',
        'classError'     => 'my-custom-danger-class',
        'classWarning'   => 'my-custom-warning-class',
        'sessionMsg'     => 'msg',
        'sessionMsgType' => 'msg_type',
        'template'       => '%s',
    ],
];
```

You can then load your custom theme automatically:

```
$alerts = service('alerts'); // Uses 'my_custom_theme'
```

Or load it explicitly by passing its key as an argument:

```
$alerts = service('alerts', 'my_custom_theme');
```

---

Usage Examples
--------------

[](#usage-examples)

### 1. Obtaining the Service Instance

[](#1-obtaining-the-service-instance)

The recommended way to instantiate the library is using CodeIgniter 4's `service()` helper:

```
// Instantiates using the default framework defined in the active property of app/Config/Alerts.php
$alerts = service('alerts');

// Instantiates forcing a specific framework override
$alertsBootstrap = service('alerts', 'bootstrap');
```

---

### 2. Setting Alert Messages (Controller)

[](#2-setting-alert-messages-controller)

You can store alert messages in session flashdata to display them after a redirect (e.g., after saving a form):

```
namespace App\Controllers;

class Products extends BaseController
{
    public function save()
    {
        // ... saving logic ...

        // Set a success alert
        service('alerts')->set('Product registered successfully!', 'success');

        // Or set an error alert
        service('alerts')->set('Could not save the product.', 'error');

        return redirect()->to('/products');
    }
}
```

---

### 3. Displaying Alert Messages (Views)

[](#3-displaying-alert-messages-views)

#### Automatic Session (Flashdata) Display

[](#automatic-session-flashdata-display)

To display the alert set in the controller, simply call the `display()` method in your view:

```
