Plugin events

blaze provides events (or hooks) which you can use to trigger plugin functionality. There are five basic events that blaze will trigger by itself.

Available basic events

'bootup'

This event will be triggered when blaze has finished loading plugins and components but before it begins to do anything.

'frontendcall'

This event will be triggered when blaze found a frontend page to show, just before the rendering of the page will be executed.

'backendcall'

This event will be triggered when blaze is about to render a backend view, just before the rendering of the view will be executed.

'backendlogin'

This event will be triggered when a user logs in to the backend (admin or customer).

'shutdown'

This event will be triggered after the output has been rendered and output. Triggering this event is the last thing blaze will do before stopping the current execution cycle.

Binding plugin functionality to events

The main class of a plugin you make must extend the class \blaze\src\protoype\Plugin which gives it two methods related to events.

    $this->bindEvent($event, $action);

This function is only available to the plugin class itself, and should be used to bind functionality to an event. A quick example for this:

Let's say you had a plugin which needs some initiating on bootup. You would create a function initate() which would do cool things. How to bind the method to the 'bootup' event? Easy.

    <?php

    namespace blaze\plugins\mycoolplugin;

    class mycoolplugin extends \blaze\src\prototype\Plugin {

        function __construct()
        {
            $this->bindEvent('bootup', 'initiate');
        }

        public function initiate()
        {
            // Do cool stuff here
        }

    }

    ?>

That's easy, isn't it? Be aware that the bound method will not be called by the plugin itself but by the plugin handler. This means, that the bound functionality must be a public method.

The example above would instantiate a plugin which is, like all plugins (given that you have a proper plugin.json in place), available by their name. Additionally, you could get the events to which the plugin bound functionality like this:

    $blaze->mycoolplugin->getBoundEvents();

Triggering custom events

You may also create your own events which you may trigger whenever you want to. Just use the bindEvent() function like shown above and provide any event you like.

blaze's central plugin handler holds one function which you may use to trigger events. If you for instance want to trigger the event 'myawesomeevent', you have to do this:

    $blaze->plugins->triggerEvent('myawesomeevent');

Keep in mind, that other plugins might also have bound functionality to this event. It is not possible to trigger an event on one plugin only - you can as well just call the functions sou want to bind directly in this case.

Using a cronjob

In lots of situations, a bit of automation is a cool thing. blaze is prepared for that - there is a special event.

Most of you should be familiar with something called "cronjob". You're not? No problem, you soon will be. A cronjob is a task, which is automatically executed by your server based on a schedule.

blaze knows about cronjobs and provides a super easy way to use them: an event. Just bind the methods you want to execute in a specific rythm to the event "cron" in your plugin

    $this->bindEvent('cron', 'myPluginMethod');

This event will be triggered in two cases. If you don't have the possibility to setup a real cronjob, it is recommended to enable the 'softcron' feature blaze provides in the backend. In this case, the 'cron' event will be triggered once a day when someone visits the website.

If you have the possibility to setup a real cronjob, you should do so, and disable the softcron in the backend settings. Just create a cronjob which calls http://yoursite.com/blaze/cron where 'yoursite.com' should be of course replaced with your website's url.

Both methods will trigger the 'cron' event and the functionality which you may have bound to the event.