Update Route.php
This commit is contained in:
parent
ad655ef2be
commit
d0a026f5d9
36
Route.php
36
Route.php
@ -2,12 +2,31 @@
|
||||
|
||||
namespace WillySoft;
|
||||
|
||||
use Exception;
|
||||
use \Exception;
|
||||
|
||||
abstract class Route
|
||||
{
|
||||
static $prefix = '';
|
||||
static $groups = [[]];
|
||||
static string $prefix = '';
|
||||
static array $groups = [
|
||||
[]
|
||||
];
|
||||
|
||||
private static function getRequestPath(): string
|
||||
{
|
||||
static $request_url_path;
|
||||
|
||||
if (isset($request_url_path)) {
|
||||
return $request_url_path;
|
||||
}
|
||||
|
||||
$request_url_path = substr_replace(
|
||||
urldecode(strtok($_SERVER['REQUEST_URI'], '?')), '', 0, strlen($_SERVER['SCRIPT_NAME'])
|
||||
);
|
||||
if ($request_url_path === '')
|
||||
$request_url_path = '/';
|
||||
|
||||
return $request_url_path;
|
||||
}
|
||||
|
||||
static function match(string $methods, string $url_path, callable $callback)
|
||||
{
|
||||
@ -16,9 +35,8 @@ abstract class Route
|
||||
array_map(strtoupper(...), explode('|', $methods))
|
||||
)) return;
|
||||
|
||||
$request_url_path_parts = explode('/', urldecode(
|
||||
strtok($_SERVER['REQUEST_URI'], '?')
|
||||
));
|
||||
$request_url_path_parts = explode('/', self::getRequestPath());
|
||||
|
||||
$url_path_parts = explode('/', self::$prefix . $url_path);
|
||||
$callback_args = [];
|
||||
for ($i = 1; $i < count($url_path_parts); $i++) {
|
||||
@ -85,10 +103,10 @@ abstract class Route
|
||||
);
|
||||
}
|
||||
|
||||
static function group(string $prefix, callable $callback)
|
||||
static function group(string $prefix = '', callable $callback)
|
||||
{
|
||||
if (!str_starts_with(
|
||||
$_SERVER['REQUEST_URI'],
|
||||
self::getRequestPath(),
|
||||
self::$prefix . $prefix
|
||||
)) return;
|
||||
self::$prefix = self::$prefix . $prefix;
|
||||
@ -97,4 +115,4 @@ abstract class Route
|
||||
array_pop(self::$groups);
|
||||
self::$prefix = rtrim(self::$prefix, $prefix);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user