Return more detailed information from router

This commit is contained in:
Kijin Sung 2020-06-18 11:23:20 +09:00
parent f8ea38c22d
commit 4c7e3dea82
2 changed files with 31 additions and 16 deletions

View file

@ -235,13 +235,19 @@ class Context
self::setRequestMethod(); self::setRequestMethod();
if (in_array(self::$_instance->request_method, array('GET', 'POST'))) if (in_array(self::$_instance->request_method, array('GET', 'POST')))
{ {
$args = Rhymix\Framework\Router::getRequestArguments(Rhymix\Framework\Router::getRewriteLevel()); $method = $_SERVER['REQUEST_METHOD'] ?: 'GET';
$url = $_SERVER['REQUEST_URI'];
$route = Rhymix\Framework\Router::getRequestArguments($method, $url, Rhymix\Framework\Router::getRewriteLevel());
self::setRequestArguments($route->args);
if ($route->status !== 200)
{
}
} }
else else
{ {
$args = array(); self::setRequestArguments();
} }
self::setRequestArguments($args);
self::setUploadInfo(); self::setUploadInfo();
// If Rhymix is installed, get virtual site information. // If Rhymix is installed, get virtual site information.

View file

@ -84,16 +84,14 @@ class Router
/** /**
* Extract request arguments from the current URL. * Extract request arguments from the current URL.
* *
* @param string $method
* @param string $url
* @param int $rewrite_level * @param int $rewrite_level
* @return array * @return object
*/ */
public static function getRequestArguments(int $rewrite_level): array public static function getRequestArguments(string $method, string $url, int $rewrite_level)
{ {
// Get the request method.
$method = $_SERVER['REQUEST_METHOD'] ?: 'GET';
// Get the local part of the current URL. // Get the local part of the current URL.
$url = $_SERVER['REQUEST_URI'];
if (starts_with(\RX_BASEURL, $url)) if (starts_with(\RX_BASEURL, $url))
{ {
$url = substr($url, strlen(\RX_BASEURL)); $url = substr($url, strlen(\RX_BASEURL));
@ -110,9 +108,13 @@ class Router
// Decode the URL into plain UTF-8. // Decode the URL into plain UTF-8.
$url = urldecode($url); $url = urldecode($url);
if ($url === '' || (function_exists('mb_check_encoding') && !mb_check_encoding($url, 'UTF-8'))) if ($url === '')
{ {
return array(); return (object)['status' => 200, 'url' => $url, 'args' => []];
}
if (function_exists('mb_check_encoding') && !mb_check_encoding($url, 'UTF-8'))
{
return (object)['status' => 404, 'url' => '', 'args' => []];
} }
// Try to detect the prefix. This might be $mid. // Try to detect the prefix. This might be $mid.
@ -132,7 +134,7 @@ class Router
{ {
$matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY); $matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY);
$allargs = array_merge(['mid' => $prefix, 'act' => $action], $matches, $args); $allargs = array_merge(['mid' => $prefix, 'act' => $action], $matches, $args);
return $allargs; return (object)['status' => 200, 'url' => $url, 'args' => $allargs];
} }
} }
@ -144,7 +146,7 @@ class Router
{ {
$matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY); $matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY);
$allargs = array_merge(['mid' => $prefix, 'act' => $action[1]], $matches, $args); $allargs = array_merge(['mid' => $prefix, 'act' => $action[1]], $matches, $args);
return $allargs; return (object)['status' => 200, 'url' => $url, 'args' => $allargs];
} }
} }
@ -152,7 +154,7 @@ class Router
if (preg_match('#^[a-zA-Z0-9_]+$#', $internal_url)) if (preg_match('#^[a-zA-Z0-9_]+$#', $internal_url))
{ {
$allargs = array_merge(['mid' => $prefix, 'act' => $internal_url], $args); $allargs = array_merge(['mid' => $prefix, 'act' => $internal_url], $args);
return $allargs; return (object)['status' => 200, 'url' => $url, 'args' => $allargs];
} }
} }
} }
@ -164,12 +166,19 @@ class Router
{ {
$matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY); $matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY);
$allargs = array_merge($route_info['extra_vars'] ?? [], $matches, $args); $allargs = array_merge($route_info['extra_vars'] ?? [], $matches, $args);
return $allargs; return (object)['status' => 200, 'url' => $url, 'args' => $allargs];
} }
} }
// If no pattern matches, return an empty array. // If no pattern matches, return an empty array.
return array(); if ($url === '' || $url === 'index.php')
{
return (object)['status' => 200, 'url' => '', 'args' => []];
}
else
{
return (object)['status' => 404, 'url' => $url, 'args' => []];
}
} }
/** /**