edit

Skytells Framework - Controller Events

What is Controller Events?

Skytells Framework's Controller events are "things" that happen in an execution point where event was called, As same as When JavaScript is used in HTML pages, JavaScript can "react" on these events. You can perform this events at your Controllers on Skytells Framework.

Controller Events Benefits

Sometime you need to inject an event function into the Core-base controller itself before or after it being executed by the low-end kernel itself, So Here are a few useful benefits to use these events.

  • Improving Code Experience
  • Inject Callable Functions to the Main-core itself
  • Hooking SF without hacking its core.

How to Perform Controller Events?

Lets say you want to execute a function when on the base controller before your extended controller being loaded, In this case you'll need to use this events and here are some examples to show you how it works.

Home Controller File:

1
2
3
4
5
6
7
<?php
Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct(); // <--- (REF 1)
    }
}

See the code above, This is your current controller right? Okay, Now by calling parent::__construct(); method, Your controller will call the ControllerBase's Constructor method to init the required components and methods used by your controller.

However, Now lets say you want to execute a function Before the base controller finishes the execution of this method, So you need to write some event like this:

Home Controller File:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  <?php
  Class MyController extends \Controller implements IController  {

    function __construct($Ref = "") {
        $this->on('load')->perform(function() {
          // Your OnLoad function .....
          echo "Hello!";
        });
        parent::__construct();
        // Outputs of parent::__construct(); is (Hello)
      }
  }

So your function is not injected to the __construct(); method for the base controller itself.

Other Example :
You can also perform such calls in a way like :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
 Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
    $this->on('load', function() {
      // Your function..
    });

    parent::__construct();
  }
 }


Now let say you want to execute a function on End, which means when the whole app's functionality being executed, Here's another example to perform this action :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
 Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
    parent::__construct();

    $this->on('end', function() {
        // Your function..
    });
  }
 }

Notice

Here we executed the onEnd event after parent::__construct() And this is the way onEnd events must be executed.

Now The Skytells Framework is going to inject your function inside the __destruct() method in the Core-base controller itself.

Callable Events

At the time, The callable Events are only on load or on end and it must be called via your Controllers.