Files
Portfolio-Skycel/includes/router.php
skycel 2c2b893558 🚀 Feature: Router PHP + Pages projets (Stories 3.2 & 3.3)
Story 3.2 - Router PHP et URLs propres:
- Router PHP léger (43 lignes) avec support {slug}
- Front controller index.php
- .htaccess pour Apache
- Pages: home, projects, project-single, skills, about, contact, 404

Story 3.3 - Page liste projets vedettes:
- Grille responsive (1→2→3 colonnes)
- Template project-card.php réutilisable
- Badges technologies (max 4 + compteur)
- Lazy loading images avec fallback SVG

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 00:07:23 +01:00

46 lines
1.1 KiB
PHP

<?php
/**
* Router simple pour URLs propres
* < 50 lignes de code
*/
class Router
{
private array $routes = [];
public function add(string $pattern, string $handler): self
{
// Convertit {param} en regex ([^/]+)
$regex = preg_replace('/\{(\w+)\}/', '([^/]+)', $pattern);
$regex = '#^' . $regex . '$#';
$this->routes[$regex] = $handler;
return $this;
}
public function resolve(string $uri): array
{
$uri = parse_url($uri, PHP_URL_PATH);
$uri = rtrim($uri, '/') ?: '/';
foreach ($this->routes as $regex => $handler) {
if (preg_match($regex, $uri, $matches)) {
array_shift($matches); // Enlève le match complet
return [$handler, $matches];
}
}
return ['pages/404.php', []];
}
public function dispatch(): void
{
$uri = $_SERVER['REQUEST_URI'] ?? '/';
[$handler, $params] = $this->resolve($uri);
// Rend les paramètres accessibles
$GLOBALS['routeParams'] = $params;
require __DIR__ . '/../' . $handler;
}
}