Skip to content

Commit

Permalink
Add where_num() method in router
Browse files Browse the repository at this point in the history
  • Loading branch information
ronmarasigan committed Dec 15, 2023
1 parent 4daaad7 commit 56807dd
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions scheme/kernel/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ private function add_route($url, $callback, $method = 'GET', $name = NULL)
if (strpos($url, '/') !== 0) {
$url = '/' . $url;
}

if(is_string($method)) {
$methods = explode('|', strtoupper($method));
} else {
$methods = $method;
}

foreach ($methods as $method) {
$route = [
'url' => $this->group_prefix . $this->sanitize_url($url),
Expand All @@ -192,7 +192,7 @@ private function add_route($url, $callback, $method = 'GET', $name = NULL)
];
$this->routes[] = $route;
}

}

/**
Expand All @@ -208,12 +208,23 @@ public function where($param, $pattern)

if ($lastRoute) {
$lastRoute['constraints'][$param] = $pattern;
$this->routes[key($this->routes)] = $lastRoute; // Update the last route in the array
$this->routes[key($this->routes)] = $lastRoute;
}

return $this;
}

/**
* Numeric
*
* @param mixed $param
* @return void
*/
public function where_numeric($param)
{
return $this->where($param, '[0-9]+');
}

/**
* Alpha
*
Expand All @@ -223,6 +234,7 @@ public function where($param, $pattern)
public function where_alpha($param)
{
return $this->where($param, '[a-zA-Z]+');

}

/**
Expand Down Expand Up @@ -296,15 +308,15 @@ private function execute_callback($url, $route)
$matches = [];
if(preg_match($this->convert_to_regex_pattern($route['url'], $route['constraints']), $url, $matches))
{
array_shift($matches); // Remove the first element (full match)
array_shift($matches);

$callback = $route['callback'];

if (is_string($callback)) {
if(strpos($callback, '::') !== false) {
[$controller, $method] = explode('::', $callback);
} else {
[$controller, $method] = [$callback, 'index'];
[$controller, $method] = [$callback, 'index'];
}
$app = APP_DIR .'controllers/'. ucfirst($controller) . '.php';
if(file_exists($app)){
Expand Down

0 comments on commit 56807dd

Please sign in to comment.