/** * Retrieve rendered contents of a controller action * * If the action results in a forward or redirect, returns empty string. * * @param string $action * @param string $controller * @param string $module Defaults to default module * @param array $params * @return string */ public function __invoke($action, $controller, $module = null, array $params = array()) { $this->resetObjects(); if (null === $module) { $module = $this->defaultModule; } // clone the view object to prevent over-writing of view variables $broker = $this->front->getHelperBroker(); $viewRenderer = $broker->load('viewRenderer'); $viewRendererClone = clone $viewRenderer; $broker->register('viewRenderer', $viewRendererClone); $this->request->setParams($params) ->setModuleName($module) ->setControllerName($controller) ->setActionName($action) ->setDispatched(true); $this->dispatcher->dispatch($this->request, $this->response); // reset the viewRenderer object to it's original state $broker->register('viewRenderer', $viewRenderer); if (!$this->request->isDispatched() || $this->response->isRedirect()) { // forwards and redirects render nothing return ''; } $return = $this->response->getBody(); $this->resetObjects(); return $return; }
/** * Matches a user submitted path. Assigns and returns an array of variables * on a successful match. * * If a request object is registered, it uses its setModuleName(), * setControllerName(), and setActionName() accessors to set those values. * Always returns the values as an array. * * @param string $path Path used to match against this routing map * @return array An array of assigned values or a false on a mismatch */ public function match($path, $partial = false) { $this->_setRequestKeys(); $values = array(); $params = array(); if (!$partial) { $path = trim($path, self::URI_DELIMITER); } else { $matchedPath = $path; } if ($path != '') { $path = explode(self::URI_DELIMITER, $path); if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) { $values[$this->_moduleKey] = array_shift($path); $this->_moduleValid = true; } if (count($path) && !empty($path[0])) { $values[$this->_controllerKey] = array_shift($path); } if (count($path) && !empty($path[0])) { $values[$this->_actionKey] = array_shift($path); } if ($numSegs = count($path)) { for ($i = 0; $i < $numSegs; $i = $i + 2) { $key = urldecode($path[$i]); $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null; $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val); } } } if ($partial) { $this->setMatchedPath($matchedPath); } $this->_values = $values + $params; return $this->_values + $this->_defaults; }
/** * Retrieve rendered contents of a controller action * * If the action results in a forward or redirect, returns empty string. * * @param string $action * @param string $controller * @param string $module Defaults to default module * @param array $params * @return string */ public function direct($action = null, $controller = null, $module = null, array $params = array()) { if ($action == null || $controller == null) { throw new \InvalidArgumentException('Action: missing argument. $action and $controller are required in action($action, $controller, $module = null, array $params = array())'); } $this->resetObjects(); if (null === $module) { $module = $this->defaultModule; } // clone the view object to prevent over-writing of view variables $viewRendererObj = HelperBroker::getStaticHelper('viewRenderer'); HelperBroker::addHelper(clone $viewRendererObj); $this->request->setParams($params)->setModuleName($module)->setControllerName($controller)->setActionName($action)->setDispatched(true); $this->dispatcher->dispatch($this->request, $this->response); // reset the viewRenderer object to it's original state HelperBroker::addHelper($viewRendererObj); if (!$this->request->isDispatched() || $this->response->isRedirect()) { // forwards and redirects render nothing return ''; } $return = $this->response->getBody(); $this->resetObjects(); return $return; }