Event System

tl;dr - You can fire events that trigger the execution of other blocks of code.

So Cora's event system offers an alternate way to execute code from the usual controller logic flow. Anything you can accomplish using events you can also accomplish without them, but there are situations where using events may make more sense than traditional controller logic.

The Basic Idea

The basic idea is that somewhere in your executed code (probably a controller), you "fire" an Event. That Event will have any number of listeners attached to it that "listen" for that event to happen. Each of those listeners are passed a copy of the Event object and then execute whatever action they are designed to implement.

Say for instance you are building a game and want a number of things to happen when a new user registers on your site:

Doing this using normal Controller based logic might look like this:

// A Controller
class Users extends \MyApp
    public function register()
    {
        // Form submission verification
        // Check that data is valid and username is available.
        // (not shown)

        // Grab registration form data
        $username = $this->input->post('username');
        $email    = $this->input->post('email');
        $password = $this->input->post('password');

        // Load an Authorization library
        $auth = new \Library\Auth();

        // Register User
        $auth->register($username, $password, $email);

        // Sign user up for newsletter
        $newsletter = new Newsletter($this->container);
        $newsletter->signup($username, $email);

        // Credit user's account with game currency
        $gameAccount = new GameAccount($this->container);
        $gameAccount->addCredit($username, 500);

        // Forward user to Welcome page
        $this->welcome();
    }
}

And honestly, if you look at the above example, it's not bad. There's perfectly nothing wrong with handling the user registration process that way. That said, if you wanted to keep your "register" method clean of extra code and focused solely on the act of registering a user, using Events is an option. Let's do the same thing again, but this time let's see what the Controller looks like with an Event: