/** * Dispatch the request to a controller::action * * @return ViewModel */ public function dispatch(Resources $context, Destination $destination = null) { /* @var $dest Destination */ $dest = $destination ?: $context->getDestination(); if (!$dest) { throw new Exception\RuntimeException("Routing destination is not set", 1); } if (!$dest->isMatch()) { $model = $this->createError($context, "The URL doesn't match any routing", 404); } else { $class = null; $params = $dest->getParams(); $action = $params->value('action') ?: "index"; $method = $this->methodName($action) . "Action"; $object = $this->controllerInstance($params, $context, $class); if (!$object) { $model = $this->createError($context, "Controller '{$class}' not found", 404); } elseif (!method_exists($object, $method)) { $model = $this->createError($context, "Method '{$method}' not found", 404); } else { $model = $this->callAction($context, $object, $method); } } return $model; }