Skip to content

Commit

Permalink
Merge pull request #5 from thecodeholic/url-params
Browse files Browse the repository at this point in the history
Add possibility to parse parameters from URL
  • Loading branch information
thecodeholic authored Feb 22, 2022
2 parents 75c27ce + 236a1f0 commit 4b106fd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
25 changes: 23 additions & 2 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace thecodeholic\phpmvc;


/**
* Class Request
*
Expand All @@ -16,6 +15,8 @@
*/
class Request
{
private array $routeParams = [];

public function getMethod()
{
return strtolower($_SERVER['REQUEST_METHOD']);
Expand Down Expand Up @@ -56,4 +57,24 @@ public function getBody()
}
return $data;
}
}

/**
* @param $params
* @return self
*/
public function setRouteParams($params)
{
$this->routeParams = $params;
return $this;
}

public function getRouteParams()
{
return $this->routeParams;
}

public function getRouteParam($param, $default = null)
{
return $this->routeParams[$param] ?? $default;
}
}
63 changes: 61 additions & 2 deletions Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,72 @@ public function post(string $url, $callback)
$this->routeMap['post'][$url] = $callback;
}

/**
* @return array
*/
public function getRouteMap($method): array
{
return $this->routeMap[$method] ?? [];
}

public function getCallback()
{
$method = $this->request->getMethod();
$url = $this->request->getUrl();
// Trim slashes
$url = trim($url, '/');

// Get all routes for current request method
$routes = $this->getRouteMap($method);

$routeParams = false;

// Start iterating registed routes
foreach ($routes as $route => $callback) {
// Trim slashes
$route = trim($route, '/');
$routeNames = [];

if (!$route) {
continue;
}

// Find all route names from route and save in $routeNames
if (preg_match_all('/\{(\w+)(:[^}]+)?}/', $route, $matches)) {
$routeNames = $matches[1];
}

// Convert route name into regex pattern
$routeRegex = "@^" . preg_replace_callback('/\{\w+(:([^}]+))?}/', fn($m) => isset($m[2]) ? "({$m[2]})" : '(\w+)', $route) . "$@";

// Test and match current route against $routeRegex
if (preg_match_all($routeRegex, $url, $valueMatches)) {
$values = [];
for ($i = 1; $i < count($valueMatches); $i++) {
$values[] = $valueMatches[$i][0];
}
$routeParams = array_combine($routeNames, $values);

$this->request->setRouteParams($routeParams);
return $callback;
}
}

return false;
}

public function resolve()
{
$method = $this->request->getMethod();
$url = $this->request->getUrl();
$callback = $this->routeMap[$method][$url] ?? false;
if (!$callback) {
throw new NotFoundException();

$callback = $this->getCallback();

if ($callback === false) {
throw new NotFoundException();
}
}
if (is_string($callback)) {
return $this->renderView($callback);
Expand Down Expand Up @@ -73,4 +132,4 @@ public function renderViewOnly($view, $params = [])
{
return Application::$app->view->renderViewOnly($view, $params);
}
}
}

0 comments on commit 4b106fd

Please sign in to comment.