PHP code example of phalcon / incubator-events

1. Go to this page and download the library: Download phalcon/incubator-events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phalcon / incubator-events example snippets


class DispatchEventsHandler
{
    public function beforeCallActionMethod(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some stuff
    }

    public function beforeDoSomeMistakes(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some right stuff
    }
}

class DispatchEventsHandlerTwo
{
    public function beforeCallActionMethod(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do another stuff in same event
    }
}

$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandlerTwo(), 101);
$eventsManager->attach('dispatch:beforeDoSomeMistakes',new DispatchEventsHandler());
//or global way
$eventsManager->attach('dispatch',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch',new DispatchEventsHandlerTwo(), 101);

class BeforeCallActionMethod
{
    public function __invoke(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some stuff
    }
}

$config = new Phalcon\Config([
  'handlers' => [
      'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
  ],
]);

$handlers = $config->get('handlers')->toArray();

$eventsManager = new Phalcon\Incubator\Events\Manager();
$eventsManager->loadHandlers($handlers);

$flat = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
    ],
]);

$verbose = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            'class' => BeforeCallActionMethod::class,
            'priority' => 100, //optional
        ],
    ],
]);

$callable = new Phalcon\Config([
    'handlers' => [
        //you can use any other public method name instead of method, for example run()
        'dispatch:beforeCallActionMethod' => [new BeforeCallActionMethod(), 'run'],
        //or
        'dispatch:beforeCallActionMethod' => [BeforeCallActionMethod::class, 'staticRun'],
    ],
]);

$grouped = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            BeforeCallActionMethod::class,
            BeforeCallActionMethodAnother::class,
            BeforeCallActionMethodThird::class,
        ],
    ],
]);

$groupedVerbose = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            [
                'class' => BeforeCallActionMethod::class,
                'priority' => 100, //optional
            ],
            [
                'class' => BeforeCallActionMethodAnother::class,
                'priority' => 101, //optional
            ],
            [
                'class' => BeforeCallActionMethodThird::class,
                'priority' => 101, //optional
            ],
        ],
    ],
]);