This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".  
Status: 2023-04-15

Events


\MVC\Event::run()

with run you initialize an event; bonded Closures to the event name get executed in order.

Example: Run a simple Event

\MVC\Event::run('foo');

Example: pass an DTArrayObject Object with Infos

\MVC\Event::run('foo', DTArrayObject::create()
    ->add_aKeyValue(DTKeyValue::create()->set_sKey('foo')->set_sValue('bar'))
);

\MVC\Event::bind()

with bind you create a Listener to an expected event; you bind a closure to the expected event.

Syntax

\MVC\Event::bind('eventName', {closure} );

Example: bind a closure to the Event "foo"

\MVC\Event::bind('foo', function(\MVC\DataType\DTArrayObject $oDTArrayObject) {
    \MVC\Debug:info($oDTArrayObject);
});
  • If the event 'foo' is triggered, the closure will be executed: the content of $oDTArrayObject will be displayed on screen

Example: bind a closure to a concrete Controller::method

\MVC\Event::bind('\{module}\Controller\Index::foo', function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
    \MVC\Debug:info($oDTArrayObject);
});
  • If the method \{module}\Controller\Index::foo is being called, the closure will be executed: the content of $oDTArrayObject will be displayed on screen
  • ⚠ Make sure to write the event name as the method was a static one, even it is not.

\MVC\Event::processBindConfigStack() - Bind to Events via config Stack

Instead of writing one bind command expressure after the other you can make use of array notation and use Event::processBindConfigStack.

This way you can note bondings to multiple Events at once and you can even note multiple closures for each event.

This reduces complexity and improves readability.

Example: using config Stack to bind closures to Events

<?php
\MVC\Event::processBindConfigStack([ // Bondings to 2 Events
    '\{module}\Controller\Index::foo' => [ // 2 Closures bonded to this Event
        function (\MVC\DataType\DTArrayObject $oDTArrayObject) {      
            \MVC\Debug:info($oDTArrayObject);
        },
        function (\MVC\DataType\DTArrayObject $oDTArrayObject) {      
            \MVC\Log:write($oDTArrayObject, 'debug.log');
        }
    ],
    '\{module}\Controller\Index::bar' => [ // 1 Closure bonded to this Event
        function (\MVC\DataType\DTArrayObject $oDTArrayObject) {      
            \MVC\Log:write($oDTArrayObject, 'debug.log');
        }
    ],    
]);

\MVC\Event::delete()

delete one or all events.

⚠ If this parameter not is set, all events are going to be deleted.

Example: delete the certain Event named foo.bar

\MVC\Event::delete('foo.bar');

Example: delete all Events

\MVC\Event::delete();

Early Event bondings

These bondings are executed right after all Configurations have been loaded. No essential classes of myMVC have been loaded or processed at this point yet.

So this is the perfect place to influence the behavior of the starting application.

Path to module's early event bonding files

module/{module}/etc/config/event/

Example: log current request object to debug.log when on develop environment

<?php

\MVC\Event::processBindConfigStack([
    'mvc.request.getCurrentRequest.after' => [
        function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
            // get request object
            $oDTRequestCurrent = $oDTArrayObject->getDTKeyValueByKey('oDTRequestCurrent')->get_sValue();

            if ('develop' === \MVC\Config::get_MVC_ENV())
            {
                \MVC\Log::write($oDTRequestCurrent, 'debug.log');
            }
        },
    ],
]);

You can write your own event bondings into an already existing file.

You can also create your own php file in that folder and note your event bondings, no problem.
⚠ but consider the loading order of the files, which is a-z.


myMVC Standard Events

Event Event::bind perforemd in Event::run located in
policy.index.requestMethodHasToMatchRouteMethod.after modules/{module}/etc/event/policy.php modules/{module}/Policy/Index.php
mvc.application.setSession.before modules/{module}/etc/event/default.php application/library/MVC/Application.php
mvc.application.setSession.after application/library/MVC/Application.php
mvc.application.construct.after application/library/MVC/Application.php
mvc.application.destruct.before application/library/MVC/Application.php
mvc.controller.init.before application/library/MVC/Request.php application/library/MVC/Controller.php
mvc.controller.construct.after @deprecated application/library/MVC/Controller.php
mvc.controller.init.after application/library/MVC/Controller.php
mvc.runTargetClassPreconstruct.after @deprecated application/library/MVC/Controller.php
mvc.controller.runTargetClassPreconstruct.after application/library/MVC/Controller.php
mvc.controller.destruct.before application/library/MVC/Controller.php
mvc.error application/library/MVC/Controller.php
application/library/MVC/Request.php
application/library/MVC/Policy.php
mvc.policy.init.before application/library/MVC/Policy.php
mvc.policy.init.after application/library/MVC/Policy.php
mvc.policy.set.before application/library/MVC/Policy.php
mvc.policy.set.after application/library/MVC/Policy.php
mvc.policy.unset.before application/library/MVC/Policy.php
mvc.policy.unset.after application/library/MVC/Policy.php
mvc.policy.apply.before application/library/MVC/Policy.php
mvc.policy.apply.execute application/library/MVC/Policy.php
mvc.reflex.reflect.before application/library/MVC/Reflex.php
mvc.reflex.reflect.targetObject.before modules/{module}/etc/event/default.php application/library/MVC/Reflex.php
mvc.reflex.reflect.targetObject.after modules/{module}/etc/event/default.php application/library/MVC/Reflex.php
mvc.reflex.destruct.before application/library/MVC/Reflex.php
mvc.route.init @deprecated application/library/MVC/Route.php
mvc.route.init.before application/library/MVC/Route.php
mvc.route.init.after application/library/MVC/Route.php
$sControllerClassName . '::' . $sMethod application/library/MVC/Reflex.php
mvc.request.getCurrentRequest.after application/library/MVC/Request.php
mvc.request.redirect application/library/MVC/Request.php
mvc.view.render.before application/library/MVC/InfoTool.php application/library/MVC/View.php
mvc.view.renderString.before application/library/MVC/View.php
mvc.view.renderString.after application/library/MVC/View.php
mvc.view.render.after application/library/MVC/View.php
mvc.debug.stop.after modules/Doc/etc/event/default.php application/library/MVC/Debug.php
mvc.lock.create application/library/MVC/Lock.php
mvc.view.echoOut.off application/library/MVC/View.php
mvc.view.echoOut.on application/library/MVC/View.php
mvc.view.render.off application/library/MVC/View.php
mvc.view.render.on application/library/MVC/View.php