Backup #490
| ID | 490 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Framework/Router.php |
| Version | 2 |
| Typ |
modified |
| Größe | 2.3 KB |
| Hash | 44f742910a41d85ff75be17f33021b6f929d542af28af257d643fb324dfb53c0 |
| Datum | 2025-12-22 15:29:41 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
namespace Framework;
class Router
{
protected array $routes = [];
public function get(string $path, array|callable $handler): self
{
return $this->add('GET', $path, $handler);
}
public function post(string $path, array|callable $handler): self
{
return $this->add('POST', $path, $handler);
}
public function put(string $path, array|callable $handler): self
{
return $this->add('PUT', $path, $handler);
}
public function delete(string $path, array|callable $handler): self
{
return $this->add('DELETE', $path, $handler);
}
public function add(string $method, string $path, array|callable $handler): self
{
$this->routes[] = [
'method' => $method,
'path' => $path,
'handler' => $handler,
];
return $this;
}
public function dispatch(string $uri, string $method): void
{
foreach ($this->routes as $route) {
if ($route['method'] !== $method) {
continue;
}
$pattern = $this->convertToRegex($route['path']);
if (preg_match($pattern, $uri, $matches)) {
array_shift($matches);
$this->handle($route['handler'], $matches);
return;
}
}
http_response_code(404);
echo '404 Not Found';
}
protected function convertToRegex(string $path): string
{
// Support {name:pattern} syntax for custom patterns (e.g., {path:.*})
$pattern = preg_replace_callback('/\{([a-zA-Z]+):([^}]+)\}/', function ($matches) {
return '(' . $matches[2] . ')';
}, $path);
// Support simple {name} syntax
$pattern = preg_replace('/\{([a-zA-Z]+)\}/', '([^/]+)', $pattern);
return '#^' . $pattern . '$#';
}
protected function handle(array|callable $handler, array $params): void
{
if (is_callable($handler)) {
call_user_func_array($handler, $params);
return;
}
[$controllerClass, $method] = $handler;
$controller = new $controllerClass();
/** @var callable $callback */
$callback = [$controller, $method];
call_user_func_array($callback, $params);
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 817 |
5 |
modified |
3.0 KB |
2025-12-23 08:09 |
| 494 |
4 |
modified |
3.0 KB |
2025-12-22 15:32 |
| 491 |
3 |
modified |
2.5 KB |
2025-12-22 15:29 |
| 490 |
2 |
modified |
2.3 KB |
2025-12-22 15:29 |
| 402 |
1 |
modified |
2.2 KB |
2025-12-22 08:53 |
← Zurück zur Übersicht