/** * Returns current request uri (the path that will be compared with routes) * * (Inspired from codeigniter URI::_fetch_uri_string method) * * @return string */ function request_uri($env = null) { static $uri = null; if (is_null($env)) { if (!is_null($uri)) { return $uri; } $env = env(); } if (array_key_exists('uri', $env['GET'])) { $uri = $env['GET']['uri']; } else { if (array_key_exists('u', $env['GET'])) { $uri = $env['GET']['u']; } else { $app_file = app_file(); $path_info = isset($env['SERVER']['PATH_INFO']) ? $env['SERVER']['PATH_INFO'] : @getenv('PATH_INFO'); $query_string = isset($env['SERVER']['QUERY_STRING']) ? $env['SERVER']['QUERY_STRING'] : @getenv('QUERY_STRING'); // Is there a PATH_INFO variable? // Note: some servers seem to have trouble with getenv() so we'll test it two ways if (trim($path_info, '/') != '' && $path_info != "/" . $app_file) { $uri = $path_info; } elseif (trim($query_string, '/') != '') { $uri = $query_string; } elseif (array_key_exists('REQUEST_URI', $env['SERVER']) && !empty($env['SERVER']['REQUEST_URI'])) { $request_uri = rtrim(rawurldecode($env['SERVER']['REQUEST_URI']), '?/') . '/'; $base_path = $env['SERVER']['SCRIPT_NAME']; if ($request_uri . "index.php" == $base_path) { $request_uri .= "index.php"; } $uri = str_replace($base_path, '', $request_uri); } elseif ($env['SERVER']['argc'] > 1 && trim($env['SERVER']['argv'][1], '/') != '') { $uri = $env['SERVER']['argv'][1]; } } } $uri = rtrim($uri, "/"); # removes ending / if (empty($uri)) { $uri = '/'; } else { if ($uri[0] != '/') { $uri = '/' . $uri; # add a leading slash } } return $uri; }
function test_main_app_file() { $app_file = strtolower(app_file()); $env = env(); assert_equal($app_file, strtolower($env['SERVER']['PWD'].'/'.$env['SERVER']['PHP_SELF'])); }
/** * Returns current request uri (the path that will be compared with routes) * * (Inspired from codeigniter URI::_fetch_uri_string method) * * @return string */ function request_uri($env = null) { static $uri = null; if (is_null($env)) { if (!is_null($uri)) { return $uri; } $env = env(); } if (array_key_exists('uri', $env['GET'])) { $uri = $env['GET']['uri']; } else { if (array_key_exists('u', $env['GET'])) { $uri = $env['GET']['u']; } else { $app_file = app_file(); $path_info = isset($env['SERVER']['PATH_INFO']) ? $env['SERVER']['PATH_INFO'] : @getenv('PATH_INFO'); $query_string = isset($env['SERVER']['QUERY_STRING']) ? $env['SERVER']['QUERY_STRING'] : @getenv('QUERY_STRING'); // Is there a PATH_INFO variable? // Note: some servers seem to have trouble with getenv() so we'll test it two ways if (trim($path_info, '/') != '' && $path_info != "/" . $app_file) { if (strpos($path_info, '&') !== 0) { # exclude GET params $params = explode('&', $path_info); $path_info = array_shift($params); # populate $_GET foreach ($params as $param) { if (strpos($param, '=') > 0) { list($k, $v) = explode('=', $param); $env['GET'][$k] = $v; } } } $uri = $path_info; } elseif (trim($query_string, '/') != '' && $query_string[0] == '/') { $uri = $query_string; $get = $env['GET']; if (count($get) > 0) { # exclude GET params $keys = array_keys($get); $first = array_shift($keys); if (strpos($query_string, $first) === 0) { $uri = $first; } } } elseif (array_key_exists('REQUEST_URI', $env['SERVER']) && !empty($env['SERVER']['REQUEST_URI'])) { $request_uri = rtrim($env['SERVER']['REQUEST_URI'], '?/') . '/'; $base_path = $env['SERVER']['SCRIPT_NAME']; if ($request_uri . "index.php" == $base_path) { $request_uri .= "index.php"; } $uri = str_replace($base_path, '', $request_uri); if (option('base_uri') && strpos($uri, option('base_uri')) === 0) { $uri = substr($uri, strlen(option('base_uri'))); } if (strpos($uri, '?') !== false) { $uri = substr($uri, 0, strpos($uri, '?')) . '/'; } } elseif ($env['SERVER']['argc'] > 1 && trim($env['SERVER']['argv'][1], '/') != '') { $uri = $env['SERVER']['argv'][1]; } } } $uri = rtrim($uri, "/"); # removes ending / if (empty($uri)) { $uri = '/'; } else { if ($uri[0] != '/') { $uri = '/' . $uri; # add a leading slash } } return rawurldecode($uri); }
function test_main_app_file() { $app_file = app_file(); $env = env(); assert_equal($app_file, $env['SERVER']['PWD'] . '/' . $env['SERVER']['PHP_SELF']); }