Developer-Guide/basics/dispatching.md

2.0 KiB

Dispatching

The dispatching is the follow up on the routing. In the dispatcher the route destination gets resolved and executed. Dispatching can be performed on module instance methods, static functions and anonymous functions.

The return of the dispatch() call is an array of all end point returns.

Basic

The dispatcher accepts a string representation of the method or static function which should be dispatched, a closure which should be executed or an array of the just mentioned options.

The dispatch() function accepts additionally a variable amount of parameters which will be passed to the routed method/function.

String

Module

The string representation for a module method is almost the same as the static function representation. The only difference is that a module method has only one colon : between the function name and the namespace.

$dispatcher->dispatch('\My\Namespace:methodToCall', $methodToCallPara1, $methodToCallPara2, ...);

Static

The string representation for a module method is almost the same as the static function representation. The only difference is that a module method has two colons :: between the function name and the namespace.

$dispatcher->dispatch('\My\Namespace::staticToCall', $staticToCallPara1, $staticToCallPara2, ...);

Closure

The closure simply takes a closure as first parameter which will be called and executed during the dispatching process.

$dispatcher->dispatch(function($para1, $para2) { ... }, $staticToCallPara1, $staticToCallPara2, ...);

Routing

The dispatcher accepts the results from the route() method of the router which is an array of routes.

$dispatcher->dispatch($router->route($request->uri->getRoute()));

Based on the function definition returned by the router it's possible to pass more parameters to the function such e.g. request and response objects.

$dispatcher->dispatch($router->route($request->uri->getRoute()), $request, $response);