function map_request_to_handler($req, $routes) { if ($route = route_match($routes, $req)) { $req['path'] = $route['path_matches']; exit_with_mojo_flush_response($req, next_func($req, $route['funcs'])); } exit_with_404_plain('Not Found'); }
/** * Dispatches incoming request. This function may trigger the 'before', * and 'after' events. * * @param string $method Request method * @param string $path Request path */ function dispatch($method, $path) { $callbacks = route(); foreach ($callbacks as $route => $methods) { if (route_match($route, $path, $matches, $redirect)) { if ($redirect) { $url = url_for($path . '/' . (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')); redirect($url, 301); } config('_route', $route); if ($method === 'HEAD') { if (isset($methods['HEAD'])) { $callback = $methods['HEAD']; } elseif (isset($methods['GET'])) { $callback = $methods['GET']; } else { $callback = null; } } elseif ($method === 'OPTIONS') { $allowed = array_keys($methods); if (in_array('GET', $allowed) && !in_array('HEAD', $allowed)) { $allowed[] = 'HEAD'; } header('Allow: ' . implode(',', $allowed)); if (isset($methods['OPTIONS'])) { $callback = $methods['OPTIONS']; } else { halt(); } } else { $callback = isset($methods[$method]) ? $methods[$method] : null; } if (!isset($callback)) { $allowed = array_keys($methods); if (in_array('GET', $allowed) && !in_array('HEAD', $allowed)) { $allowed[] = 'HEAD'; } header('Allow: ' . implode(',', array_keys($methods))); halt(405); } $params = array(); foreach ($matches as $key => $val) { if (is_string($key)) { params($key, $params[$key] = urldecode($val)); } } $pass = false; try { ($before = before()) && apply($before, $params); apply($callback, $params); ($after = after()) && apply($after, $params); } catch (\PassException $e) { $pass = true; } if (!$pass) { return; } } } halt(404); }
/** * @internal * * Route to a page according to request * interally called by /app/index.php * * @return string */ function router() { global $lc_homeRouting; # Get a route from the defined custom routes (if any) $_page = route_match(); if ($_page) { return route_getAbsolutePathToRoot($_page); } $q = route_path(); # if the route is empty, get it from the config if (empty($q) && $lc_homeRouting) { $q = $lc_homeRouting; } # if it is still empty, set it to the system default if (empty($q)) { $q = 'home'; } # Get the complete path to root $_page = route_getAbsolutePathToRoot($q); if (!empty($_page) && file_exists($_page)) { return $_page; } if (preg_match('/(.*)(401|403|404) {1}$/', $_page, $matches)) { return _i('inc/tpl/' . $matches[2] . '.php'); } # Search the physical directory according to the routing path $_page = route_search(); if ($_page && is_file($_page) && file_exists($_page)) { return $_page; } if (in_array(_arg(0), array('401', '403'))) { return _i('inc/tpl/' . _arg(0) . '.php'); } else { return _i('inc/tpl/404.php'); } }
public function testRouteMatchMethodError() { try { $_GET[ROUTE] = 'post/create'; route_match(); } catch (\Exception $e) { $this->assertEqual(get_class($e), 'RuntimeException'); $this->assertEqual($e->getMessage(), 'The Router does not allow the method "GET" for "lc_post_create".'); } try { $_GET[ROUTE] = 'post/1/update'; route_match(); } catch (\Exception $e) { $this->assertEqual(get_class($e), 'RuntimeException'); $this->assertEqual($e->getMessage(), 'The Router does not allow the method "GET" for "lc_post_update".'); } }