public function route(Zend_Controller_Dispatcher_Interface $dispatcher) { /** * @todo Replace with Zend_Request object */ $path = $_SERVER['REQUEST_URI']; if (strstr($path, '?')) { $path = substr($path, 0, strpos($path, '?')); } /** * Find the matching route */ foreach ($this->_routes as $route) { if ($params = $route->match($path)) { $controller = $params['controller']; $action = $params['action']; break; } } $actionObj = new Zend_Controller_Dispatcher_Token($controller, $action, $params); if (!$dispatcher->isDispatchable($actionObj)) { throw new Zend_Controller_Router_Exception('Request could not be mapped to a route.'); } else { return $actionObj; } }
public function route(Zend_Controller_Dispatcher_Interface $dispatcher) { /** * @todo Replace with Zend_Request object */ $path = $_SERVER['REQUEST_URI']; if (strstr($path, '?')) { $path = substr($path, 0, strpos($path, '?')); } $path = explode('/', trim($path, '/')); /** * The controller is always the first piece of the URI, and * the action is always the second: * * http://zend.com/controller-name/action-name/ */ $controller = $path[0]; $action = isset($path[1]) ? $path[1] : null; /** * If no controller has been set, IndexController::index() * will be used. */ if (!strlen($controller)) { $controller = 'index'; $action = 'index'; } /** * Any optional parameters after the action are stored in * an array of key/value pairs: * * http://www.zend.com/controller-name/action-name/param-1/3/param-2/7 * * $params = array(2) { * ["param-1"]=> string(1) "3" * ["param-2"]=> string(1) "7" * } */ $params = array(); for ($i = 2; $i < sizeof($path); $i = $i + 2) { $params[$path[$i]] = isset($path[$i + 1]) ? $path[$i + 1] : null; } $actionObj = new Zend_Controller_Dispatcher_Token($controller, $action, $params); if (!$dispatcher->isDispatchable($actionObj)) { /** * @todo error handling for 404's */ throw new Zend_Controller_Router_Exception('Request could not be mapped to a route.'); } else { return $actionObj; } }
/** * 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 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 = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); Zend_Controller_Action_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 Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj); 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) { $this->_setRequestKeys(); $values = array(); $params = array(); $path = trim($path, self::URI_DELIMITER); 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; } } } $this->_values = $values + $params; return $this->_values + $this->_defaults; }
public function route(Zend_Controller_Dispatcher_Interface $dispatcher) { /** * @todo Replace with Zend_Request object */ $path = $_SERVER['REQUEST_URI']; if (strstr($path, '?')) { $path = substr($path, 0, strpos($path, '?')); } // Remove RewriteBase if (strlen($this->_rewriteBase) > 0 && strpos($path, $this->_rewriteBase) === 0) { $path = substr($path, strlen($this->_rewriteBase)); } /** * Find the matching route */ $controller = 'index'; $action = 'noRoute'; foreach (array_reverse($this->_routes) as $route) { if ($params = $route->match($path)) { $controller = $params['controller']; $action = $params['action']; $this->_currentRoute = $route; break; } } $actionObj = new Zend_Controller_Dispatcher_Token($controller, $action, $params); if (!$dispatcher->isDispatchable($actionObj)) { throw new Zend_Controller_Router_Exception('Request could not be mapped to a route.'); } else { return $actionObj; } }
/** * Return the dispatcher object. * * @return Zend_Controller_Dispatcher_Interface */ public function getDispatcher() { /** * Instantiate the default dispatcher if one was not set. */ if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) { require_once 'Zend/Controller/Dispatcher/Standard.php'; $this->_dispatcher = new Zend_Controller_Dispatcher_Standard(); $this->_dispatcher->setFrontController($this); } return $this->_dispatcher; }
/** * 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) { $this->_setRequestKeys(); $values = array(); $params = array(); $path = trim($path, self::URI_DELIMITER); 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; /* * check if key is meant as array as in index/index/key[]/one/key[]/two/key[]/three */ if (substr($key, -2) === '[]') { //key is an array index $index = substr($key, 0, strlen($key) - 2); if (array_key_exists($index, $params)) { if (is_array($params[$index])) { //is already an array, ad value $params[$index][] = $val; } else { //the key is already registered, it will not be overwritten } } else { //create array at $index and place first value $params[$index] = array($val); } } else { //regular key $params[$key] = $val; } } } } $this->_values = $values + $params; return $this->_values + $this->_defaults; }
/** * 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) { $this->_setRequestKeys(); $values = array(); $params = array(); $path = trim($path, self::URI_DELIMITER); 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; /* * check if key is meant as array as in index/index/var/one/var/two/var/three * or even index/index/var[]/one/var[]/two */ if (array_key_exists($key, $params)) { if (is_array($params[$key])) { //is already an array, ad value $params[$key][] = $val; } else { //save first element $first_element = $params[$key]; //replace key value with array with key value as first element $params[$key] = array($first_element); //value is next element $params[$key][] = $val; } } else { //regular key $params[$key] = $val; } } } } $this->_values = $values + $params; return $this->_values + $this->_defaults; }
/** * Initialize the class instance variables and then call the action. * * @param Zend_Controller_Dispatcher_Token $action */ public final function run(Zend_Controller_Dispatcher_Interface $dispatcher, Zend_Controller_Dispatcher_Token $action) { $this->_action = $action; $this->_params = $action->getParams(); if (!strlen($action->getActionName())) { $action->setActionName('index'); } $methodName = $dispatcher->formatActionName($action->getActionName()); if (!method_exists($this, $methodName)) { $this->__call($methodName, array()); } else { $method = new ReflectionMethod($this, $methodName); if ($method->isPublic() && !$method->isStatic()) { $this->{$methodName}(); } else { throw new Zend_Controller_Page_Exception('Illegal action called.'); } } $nextAction = $this->_nextAction; $this->_nextAction = null; return $nextAction; }
/** * 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) { $this->_setRequestKeys(); if ($this->hasTranslator()) { $translateMessages = $this->getTranslator()->getMessages(); } $values = array(); $params = array(); if (!$this->isPartial()) { $path = trim($path, self::URI_DELIMITER); } if ($path != '') { $path = explode(self::URI_DELIMITER, $path); if ($this->_dispatcher && ($this->_dispatcher->isValidModule($path[0]) || $this->hasTranslator() && !empty($translateMessages) && false !== ($moduleTranslated = array_search($path[0], $translateMessages)) && $this->_dispatcher->isValidModule($moduleTranslated))) { $module = array_shift($path); if ($this->hasTranslator() && !empty($translateMessages)) { $moduleTranslated = array_search($module, $translateMessages); if (!empty($moduleTranslated)) { $module = $moduleTranslated; } } $values[$this->_moduleKey] = $module; $this->_moduleValid = true; } if (count($path) && !empty($path[0])) { $controller = array_shift($path); if ($this->hasTranslator() && !empty($translateMessages)) { $controllerTranslated = array_search($controller, $translateMessages); if (!empty($controllerTranslated)) { $controller = $controllerTranslated; } } $values[$this->_controllerKey] = $controller; } if (count($path) && !empty($path[0])) { $action = array_shift($path); if ($this->hasTranslator() && !empty($translateMessages)) { $actionTranslated = array_search($action, $translateMessages); if (!empty($actionTranslated)) { $action = $actionTranslated; } } $values[$this->_actionKey] = $action; } 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 ($this->isPartial()) { $this->setMatchedPath($path); } $this->_values = $values + $params; return $this->_values + $this->_defaults; }
public static function cloneFromDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher) { $new = new self($dispatcher->getParams()); $new->setControllerDirectory($dispatcher->getControllerDirectory()); $new->setDefaultModule($dispatcher->getDefaultModule()); $new->setDefaultControllerName($dispatcher->getDefaultControllerName()); $new->setDefaultAction($dispatcher->getDefaultAction()); $new->setPathDelimiter($dispatcher->getPathDelimiter()); return $new; }
/** * 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) { $numbers = new Ml_Model_Numbers(); if (HOST_MODULE == "api") { return array("controller" => "notstatic", "action" => "error", "module" => "api"); } else { if (HOST_MODULE == "default") { //@todo this is a workaround for another resource: the tag system //the Zend_Controller_Router_Route_Regex don't work with utf-8 //despite trying the hack http://framework.zend.com/issues/browse/ZF-6661 //it didn't work //clear this part of the code when everything is ok //and only let the static=>docs $path = explode("/", $path, 5); if (isset($path[2]) && $path[2] == "tags" && isset($path[3])) { //could be using regex... $username = $path[1]; if ($username) { $tag = urldecode($path[3]); if (!isset($path[4])) { $page = "1"; } else { if (mb_substr($path[4], 0, 4) == "page") { $tryPage = mb_substr($path[4], 4); if ($numbers->isNaturalDbId($tryPage)) { $page = $tryPage; } } } if (isset($page)) { return array("username" => $username, "tag" => $tag, "page" => $page, "controller" => "tagspages", "action" => "tagpage", "module" => "default"); } } } //end of workaround return array("controller" => "static", "action" => "docs", "module" => "default"); } } $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; }
public function route(Zend_Controller_Dispatcher_Interface $dispatcher, Zend_Uri_Http $url) { $params = false; foreach ($this->routes as $route) { $params = $route->isMatch($url); if ($params !== false) { break; } } if ($params) { $controller = 'index'; $action = 'index'; if (isset($params['controller']) && strlen($params['controller'])) { $controller = $params['controller']; if (isset($params['action'])) { $action = $params['action']; } } unset($params['controller'], $params['action']); $token = new Zend_Controller_Dispatcher_Token($controller, $action, $params); if ($dispatcher->isDispatchable($token)) { return $token; } else { throw new Zend_Controller_Router_Exception('Request could not be mapped to a dispatchable route.'); } } else { throw new Zend_Controller_Router_Exception('Request could not be mapped to a route.'); } }