The Heart of Your Product

Documentation

Download in PDF

Download local copy of SCore docs in one file

ZF Documentation

Read It

Explore ZF manuals for native features

Table of Contents

Events & Handlers

There is smart way to create and execute events in SingularCore.

All of handler files must be placed into particular module child folder "handlers" how this shown on the image 1.


You may name handler files as you want. But it recommended to name handler files with functionality sense keyword what this class do.

Skeleton class example in handler file:

class Home_Handler_Main extends Singular_Event_Listener_Abstract
{
    public function bindListeners(Singular_Event_Dispatcher $dispatcher)
    {
        // your listeners
    }
}

Handler class name is consists of <moduleName>_Handler_<handlerFileName> and must be extending Singular_Event_Listener_Abstract class. Besides it class must contain public method bindListeners(Singular_Event_Dispatcher $dispatcher) with one argument "dispatcher" as an instance of Singular_Event_Dispatcher object.

Then create one or more arbitrary methods that are describes handlers and call this methods from bindListeners method via Singular_Event_Dispatcher->addListener($eventName, $handler = null). $handler attribute must be an array with two values: array($handlerObject, $method).

Handler class example:

class Test_Handler_Menu extends Singular_Event_Listener_Abstract
{
    public function bindListeners(Singular_Event_Dispatcher $dispatcher)
    {
        $dispatcher->addListener('adminMainMenu', array($this, 'onAdminMainMenu'));
        $dispatcher->addListener('adminSubMenu', array($this, 'onAdminSubMenu'));
    }

    /**
     * Adds pages to admin main menu
     * 
     * @param Singular_Wireframe_Frame_Admin_MainMenu $menu
     * @return void
     */
    public function onAdminMainMenu ($menu)
    {
        $menu->addPage(
            array(
                 'id' => 'testmod',
                 'label' => 'Test module',
                 'icon' => $this->view->imageSrc('icons/test.png', 'test'),
                 'module' => 'test',
                 'controller' => 'admin',
                 'action' => 'index'
            ), 'custom');
    }

    /**
     * @param Singular_Wireframe_Frame_Admin_SubMenu $menu
     * @return void
     */
    public function onAdminSubMenu ($menu)
    {
        $menu->addPage(
            array(
                 'id' => 'modules_structure',
                 'label' => 'Modules structure',
                 'module' => 'system',
                 'controller' => 'structure'
            ), 'structure');
    }
}

Full example with event dispatching:

class Home_Handler_Main extends Singular_Event_Listener_Abstract
{
    private $test = array(0 => 'hello', 1 => 'world');
    
    public function bindListeners(Singular_Event_Dispatcher $dispatcher)
    {
        $dispatcher->addListener('beforeTest', array($this, 'onBeforeTest'));
        $dispatcher->addListener('afterTest', array($this, 'onAfterTest'));
    }
    
    public function onBeforeTest ($data)
    {
        var_dump($data);
        $data = $this->test;
    }
    
    public function onAfterTest ($data)
    {
        var_dump($data);
    }
}   

// sample data
$test = array('hi', 'there');

// dispatch "beforeTest" event
Singular_Event::dispatch('beforeTest', $test);

// check for "beforeTest" is dispatched
if (Singular_Event::isDispatched('beforeTest')) {
    echo "
Dispatched!
"; } // dispatch "afterTest" event Singular_Event::dispatch('afterTest', $test);

Available events

Event name

Input parameter / description

Usage area

routeStartup

$request
Zend_Controller_Request_Abstract

admin / frontend

routeShutdown

$request
Zend_Controller_Request_Abstract

admin / frontend

dispatchLoopStartup

$request
Zend_Controller_Request_Abstract

admin / frontend

preDispatch

$request
Zend_Controller_Request_Abstract

admin / frontend

postDispatch

$request
Zend_Controller_Request_Abstract

admin / frontend

postDispatchOnce

$request
Zend_Controller_Request_Abstract

Executes postDispatch event only once, no matter how many times dispatcher process runs.

admin / frontend

dispatchLoopShutdown

$request
Zend_Controller_Request_Abstract

admin / frontend

taxonomyModuleUpdate

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

taxonomyRecordUpdate

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

taxonomySectionUpdate

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

taxonomyModuleInsert

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

taxonomyRecordInsert

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

taxonomySectionInsert

$activeRecord

Singular_Taxonomy_ActiveRecord

admin / frontend

componentRender

$abstract

Singular_Component_Abstract

Runs before component render process

admin / frontend