/** * urlTo is a helper method that returns the url to a controller method. * Examples: * $controller->urlTo('someMethod'); => /route/to/someMethod/ * $controller->urlTo('someMethodOneParameter', 'param1'); => /route/to/someMethodOneParam/param1 * $controller->urlTo('OtherController::otherMethod'); => Returns the route to another controller's method * * Thanks to Joshua Paine for improving the API of urlTo! * * @param $methodName * @return string The url linking to controller method. */ public function urlTo($methodName) { $args = func_get_args(); // First check to see if this is a urlTo on another Controller Class if (strpos($methodName, '::') !== false) { return call_user_func_array(array($this->application, 'urlTo'), $args); } array_shift($args); $descriptor = Controller::getClassDescriptor($this); if (isset($descriptor->methodUrls[$methodName])) { $url = $descriptor->methodUrls[$methodName]; if ($url[0] != '/') { $url = $this->application->routingPrefix . $url; } else { $url = substr($url, 1); } if (!empty($args)) { $reflectedMethod = new ReflectionMethod($this, $methodName); $parameters = $reflectedMethod->getParameters(); if (count($parameters) < count($args)) { throw new RecessException('urlTo(\'' . $methodName . '\') called with ' . count($args) . ' arguments, method "' . $methodName . '" takes ' . count($parameters) . '.', get_defined_vars()); } $i = 0; $params = array(); foreach ($parameters as $parameter) { if (isset($args[$i])) { $params[] = '$' . $parameter->getName(); } $i++; } $url = str_replace($params, $args, $url); } if (strpos($url, '$') !== false) { throw new RecessException('Missing arguments in urlTo(' . $methodName . '). Provide values for missing arguments: ' . $url, get_defined_vars()); } return trim($_ENV['url.base'] . $url); } else { throw new RecessException('No url for method ' . $methodName . ' exists.', get_defined_vars()); } }
/** * urlTo is a helper method that returns the url to a controller method. * Examples: * $controller->urlTo('someMethod'); => /route/to/someMethod/ * $controller->urlTo('someMethodOneParameter', 'param1'); => /route/to/someMethodOneParam/param1 * $controller->urlTo('OtherController::otherMethod'); => Returns the route to another controller's method * * Thanks to Joshua Paine for improving the API of urlTo! * * @param $methodName * @return string The url linking to controller method. */ public function urlTo($methodName) { $args = func_get_args(); // First check to see if this is a urlTo on another Controller Class if (strpos($methodName, '::') !== false) { return call_user_func_array(array($this->application, 'urlTo'), $args); } // Check to see if this urlTo contains args in the methodName // Ignores keys, assuming params are in the proper order. // Ex. $controller->urlTo("details?id=43") if (strpos($methodName, '?') !== false) { list($methodName, $params) = explode('?', $methodName, 2); $args = empty($args) ? explode('&', $params) : $args; foreach ($args as $i => $arg) { $val = strpos($arg, '=') !== false ? substr($arg, strrpos($arg, '=') + 1) : $arg; $args[$i] = $val; } } array_shift($args); $descriptor = Controller::getClassDescriptor($this); if (isset($descriptor->methodUrls[$methodName])) { $url = $descriptor->methodUrls[$methodName]; if ($url[0] != '/') { $url = $this->application->routingPrefix . $url; } else { $url = substr($url, 1); } if (!empty($args)) { $reflectedMethod = new ReflectionMethod($this, $methodName); $parameters = $reflectedMethod->getParameters(); if (count($parameters) < count($args)) { throw new RecessException('urlTo(\'' . $methodName . '\') called with ' . count($args) . ' arguments, method "' . $methodName . '" takes ' . count($parameters) . '.', get_defined_vars()); } $i = 0; $params = array(); foreach ($parameters as $parameter) { if (isset($args[$i])) { $params[] = '$' . $parameter->getName(); } $i++; } $url = str_replace($params, $args, $url); } if (strpos($url, '$') !== false) { throw new RecessException('Missing arguments in urlTo(' . $methodName . '). Provide values for missing arguments: ' . $url, get_defined_vars()); } return trim($_ENV['url.base'] . $url); } else { throw new RecessException('No url for method ' . $methodName . ' exists.', get_defined_vars()); } }