CodeIgniter 4 Events: Build Decoupled and Scalable Apps⚡

Reading Time: 4 minutes
2 Views

Events are a powerful feature in any modern framework that allow you to execute specific pieces of code when certain actions happen — without tightly coupling them to the core logic. In CodeIgniter 4, the Events system makes it easy to build scalable, clean, and decoupled applications by separating logic into event listeners.

In this tutorial, we’ll cover how to register, trigger, and listen to events in CodeIgniter 4 with step-by-step examples, and how to generate event listener classes using the Spark CLI.

Let’s get started.

Read More: CodeIgniter 4 Caching Techniques (Boost Speed Instantly!) 🚀

⚪ What Are Events in CodeIgniter 4?

Events are simple hooks that fire when a specific action takes place. They’re often used for activities like logging, sending notifications, or running background tasks after something happens — for example, after a user registers or uploads a file.

Benefits of Events:

  • Keeps core logic clean and organized.
  • Allows extra features without modifying main functions.
  • Improves scalability and flexibility for your applications.

⚪ Defining Events and Listeners

In CodeIgniter 4, you register your event listeners inside:

app/Config/Events.php

Example Registration:

Events::on('user_registered', 'App\Listeners\SendWelcomeEmail::handle');

This means whenever the user_registered event is triggered in the app, it will run the handle method inside the SendWelcomeEmail listener class.

⚪ Creating an Event Listener

You can manually create a listener class in the app/Listeners/ directory, or use the Spark CLI to generate it automatically.

📍 Using Spark CLI to Generate Listener:

php spark make:listener SendWelcomeEmail

📍 This command creates:

app/Listeners/SendWelcomeEmail.php

📍 Listener Example:

<?php
namespace App\Listeners;

class SendWelcomeEmail
{
    public static function handle($data)
    {
        // Example: send a welcome email or log it
        log_message('info', 'Welcome email sent to: ' . $data['email']);
    }
}

This handle method will automatically receive the data passed by the event trigger.

⚪ Triggering Events

Once your event and listener are registered, you can trigger an event from anywhere in your CodeIgniter 4 application using:

Events::trigger('user_registered', ['email' => 'user@example.com']);

Example in a Controller:

use CodeIgniter\Events\Events;

public function createUser()
{
    // Assume user creation logic here...

    Events::trigger('user_registered', ['email' => 'newuser@example.com']);
}

When this trigger runs, it automatically calls the handle method in SendWelcomeEmail, passing the email address.

⚪ Example Use Cases for Events

Events are great for decoupling background or secondary tasks like:

  • 📧 Sending notification emails after user registration.
  • 📝 Logging activity after critical actions.
  • ⚙️ Running asynchronous background processes.
  • 📊 Integrating third-party services like SMS, payment APIs, or analytics tools.

⚪ Conclusion

The CodeIgniter 4 Events system is a clean and effective way to manage extra actions in your app without cluttering the core logic. By setting up event listeners and triggers, you can build scalable, organized, and flexible applications with ease.

Using Spark CLI to generate listeners further saves time and keeps your project structure clean. Start using events to handle post-actions like notifications, logs, or background jobs — without touching your main business logic.