Dispatching is the process of loading a specific module and executing a specific action with given parameters. The dispatcher is essentially a middleman - it is a way to decouple the Request and URL Router from having to know how to load and execute modules. It is also a convenient way to perform Hierarchical MVC sub-requests on demand. The equivalent command in English might sound something like "load this module, run this action with these parameters, and return the result".

Dispatching in Alloy is done from the Kernel object. Any Controller, view template, or other object that has access to the Kernel can perform a dispatch to execute and return a module action result.

Dispatch Methods

In Alloy, there are two different types of dispatches, and two primary ways the dispatcher can be invoked. The primary and most common way the dispatcher is invoked is through mapping a specific URL to a named module and action with the URL Router. The bootstrap file takes the module and action parsed from the matched URL route and runs them through the dispatcher to execute them, which then returns the response from the module's Controller method.

dispatchRequest($module [, $action, array $params])

Primary dispatch method that is run for direct web requests. Using this dispatch method will do two things:

  1. Resolve the given named action to it's appropriate Controller method based on the request context. An example is when the URL route determines that the "Events" module should be loaded and the "index" action should be called. This translates to the "Module\Events\Controller" object being instantiated and the "indexAction" method being executed.
  2. Pass in the Request object instance as the first parameter. When the "indexAction" method is executed, it looks more like "indexAction($request)". Any other parameters specified by calling this method directly will be set on the Request object instance. The Request object is the only argument that will be passed in order to maintain the same interface that direct HTTP requests operate on.

# Will resolve this as 'indexAction($request)' echo $kernel->dispatchRequest('Events_Recommended', 'index'); # Will resolve this as 'indexAction($request, $lat, $lon)' echo $kernel->dispatchRequest('Restaurants_Nearby', 'index', array($event->lat, $event->lon));


dispatch($module, $action [, array $params])

Direct dispatch method for exact module + action + params dispatching. This method does not make any attempt to translate the dispatch request or add in any extra parameters.

# Raw, direct internal request. No modifications are made. echo $kernel->dispatch('Reviews', 'listForType', array('event', $event->id));


Related Links