static function render($type, $value = null, $params = array()) { if ($type === false) { self::$params['nothing'] = true; return; } if (is_int(strpos($type, '#'))) { /** * We're rendering a controller/action file. * In this case, $value holds the params, and we only change * the 'render' value and return. We can't call the * render file within this or any function because of variables scope. * * This is expected to occur in a controller, therefore in the controller one must * also return; after calling this function, so further code won't be executed. */ list($ctrl, $act) = explode('#', parse_url_token($type)); self::parse_parameters($value); self::$render = VIEWPATH . "{$ctrl}/{$act}.php"; return; } # Running after-filters. ActionController::run_after_filters(); self::parse_parameters($params); if ($type == 'json') { header('Content-Type: application/json; charset=utf-8'); if (is_array($value)) { $value = to_json($value); } echo $value; exit; } elseif ($type == 'xml') { header('Content-type: application/rss+xml; charset=UTF-8'); if (is_array($value) || is_object($value)) { $root = isset($params['root']) ? $params['root'] : 'response'; $value = to_xml($value, $root); } echo $value; exit; } elseif ($type == 'inline') { self::$params['render_type'] = 'inline'; self::$params['render_value'] = $value; include SYSROOT . 'action_view/render_markup_default.php'; } }
/** * URL for controller#action * * Parses a controller#action token and return the URL based on routes. * It can also receive any URL, i.e. '/post', 'www.google.com', etc. * * Special $opts: * compare_current_uri (bool): If the current Controller/Action matches the $url token, * the current URL (with no get params) is returned. * anchor (string): anchor for the url (i.e. /post/show/1234#c24) */ function url_for($url, $opts = array()) { // global $ActionController; // vde($opts); $routes =& ActionController::$routes; $params = null; $used_params = array(); if (!empty($opts['anchor'])) { $anchor = $opts['anchor']; unset($opts['anchor']); } else { $anchor = null; } if (is_bool(strpos($url, '/')) && is_bool(strpos($url, 'www.'))) { # Complete the given $url. $url = parse_url_token($url); if (!empty($opts['compare_current_uri'])) { list($url_ctrl, $url_act) = explode('#', $url); if ($url_ctrl == Request::$controller && $url_act == Request::$action) { return Request::$url; } else { unset($url_ctrl, $url_act, $opts['compare_current_uri']); } } list($ctrl, $act) = explode('#', $url); if (array_key_exists($url, $routes)) { # $root isn't an array, there may be more like it. if (!is_array($routes[$url])) { $route = $routes[$url]; } else { $route = $routes[$url][0]; } /** * Replacing variables in route for $opts */ # First check if url requires variables, and if they're in $opts # This is simmilar to 'Parse variables' in ActionController::routing() if (is_int(strpos($route, '$'))) { preg_match_all('/\\$([\\w]+)/', $route, $vars); $vars = $vars[1]; foreach ($vars as $var) { // # If url requires parameters and they're not in options, // # return (or throw an exception). // if (!isset($opts[$var])) // Missing parameter for route. // die('Missing parameter for route.'); if (!isset($opts[$var])) { // Missing parameter for route. $opts[$var] = false; } else { # If requirement isn't met, return (or throw an exception). if (isset($routes[$url]['requirements'][$var])) { $req = $routes[$url]['requirements'][$var]; if (!preg_match('~^' . $req . '$~', $opts[$var])) { die('Requirement not met for route parameter.'); } // Requirement not met for route parameter. } } $used_params[] = $var; $bl = $opts[$var] === false ? '(?:\\/)?' : null; $route = preg_replace('~' . $bl . '\\$' . addslashes($var) . '~', $opts[$var], $route); } } } else { foreach ($routes as $k => $route) { if (!is_int($k)) { continue; } elseif (is_array($route)) { $r = $route[0]; } else { $r = $route; } preg_match_all('/\\$([\\w]+)/', $r, $vars); $vars = $vars[1]; /* Find the matching route by checking the route's parameters. * All the route's parameters must be set in $opts and must * match requirement (if exists), otherwise the route is discarted. */ foreach ($vars as $var) { $route_opts = $opts; $used_params = array(); // vd($opts, 1); // vd($var, 2); if ($var == 'controller' || $var == 'action') { # We wont't need controller or action anymore. $used_params[] = $var; continue; } elseif ($var == 'format') { # We might need format later. continue; } // # Check if $$ctrl->$var exists to use it in params only if no options were given. if (!isset($route_opts[$var])) { // global ${$ctrl}; // if (empty($opts) && isset(${$ctrl}->$var)) { // $route_opts[$var] = ${$ctrl}->$var; // } else $route_opts[$var] = false; } // vde($vars); if (isset($route['requirements'][$var]) && !preg_match('~^' . $route['requirements'][$var] . '$~', $route_opts[$var])) { continue 2; } // vd($var); $used_params[] = $var; $bl = $route_opts[$var] === false ? '(?:\\/)?' : null; $r = preg_replace('~' . $bl . '\\$' . addslashes($var) . '~', $route_opts[$var], $r); break; } // vd($r); $opts = array_merge($opts, $route_opts); # Replace $controller, $action and .$format in $r. if (is_int(strpos($r, '$controller'))) { $r = str_replace('$controller', $ctrl, $r); } if (is_int(strpos($r, '$action'))) { # If action is 'index', we don't need to show so in the url. if ($act == 'index') { $r = str_replace(array('/$action', '$action'), '', $r); } else { $r = str_replace('$action', $act, $r); } } if (strstr($r, '.$format')) { # If format is in route, we won't need it later. $used_params[] = 'format'; if (empty($opts['format']) || $opts['format'] == 'html') { $opts['format'] = null; } $r = str_replace('.$format', $opts['format'], $r); } $found = true; break; } if (!isset($found)) { die('Unable to find route for redirection'); } $route = $r; } $route = '/' . $route; } else { $route = $url; } // if (isset($opts['id']) && $opts['id'] == 22) { // $used_params; // } // vd($used_params); $opts_left = array_diff(array_keys($opts), $used_params); if ($opts_left) { foreach ($opts_left as $param) { $params[] = $param . '=' . u($opts[$param]); } $params = '?' . implode('&', $params); } $anchor && ($route .= '#' . $anchor); return $route . $params; }