Skip to content

Commit

Permalink
Fix route regex detection and passing of parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Ilic <[email protected]>
  • Loading branch information
gigili committed Mar 31, 2021
1 parent 0dd303a commit c45e768
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function add(string $url = "", callable|string|null $callback = NULL, arr
if (str_contains($url, ":")) {
$nUrl = preg_replace("/(:[\w\-_]+)/", "([\w\-\_\:]+)", $url);
$nUrl = str_replace("/", "\/", $nUrl);
$nUrl .= "$";
}

$this->routes[$url] = [
Expand Down Expand Up @@ -87,18 +88,18 @@ public function route(): bool {
}

foreach ($this->routes as $route) {
if (is_null($route["regex"]) === FALSE) {
if (!is_null($route["regex"])) {
if (preg_match("/^{$route["regex"]}/", $url) === 1) {
$urlIndex = $route["url"];

preg_match_all("/^{$route["regex"]}-{$_SERVER['REQUEST_METHOD']}/", $url, $tmpParams);
preg_match_all("/^{$route["regex"]}-{$_SERVER['REQUEST_METHOD']}/", $route["url"], $paramNames);
preg_match_all("/^{$route["regex"]}/", $url, $tmpParams);
preg_match_all("/^{$route["regex"]}/", $route["url"], $paramNames);
array_shift($tmpParams);
array_shift($paramNames);

$params = [];
for ($x = 0; $x < count($paramNames); $x++) {
$params[str_replace(":", "", $paramNames[$x][0])] = $tmpParams[$x][0];
$params[str_replace(":", "", $paramNames[$x][0] ?? "")] = $tmpParams[$x][0] ?? "";
}

if (is_array($this->routes[$urlIndex]["params"])) {
Expand Down

0 comments on commit c45e768

Please sign in to comment.