This commit is contained in:
julien
2026-03-09 15:14:57 +01:00
parent 50831ea83a
commit 4d678f1211
8 changed files with 86 additions and 30 deletions

View File

@@ -30,6 +30,33 @@ class PostController
return $this->view->render($res, 'pages/home.twig', ['posts' => $posts]);
}
/**
* Affiche un article complet par son slug.
*/
public function show(Request $req, Response $res, array $args): Response
{
$slug = (string)($args['slug'] ?? '');
// Récupérer tous les posts et chercher celui qui correspond au slug
$rows = $this->db->select('post', '*', ['ORDER' => ['id' => 'DESC']]);
$post = null;
foreach ($rows ?: [] as $row) {
$postObj = Post::fromArray($row);
if ($postObj->getSlug() === $slug) {
$post = $postObj;
break;
}
}
if (!$post) {
$res->getBody()->write('Article non trouvé');
return $res->withStatus(404);
}
return $this->view->render($res, 'pages/post_detail.twig', ['post' => $post]);
}
/**
* Affiche la page d'administration.
*/

View File

@@ -135,18 +135,6 @@ final class Post
return trim($slug, '-');
}
/**
* Indique si l'article a été créé récemment.
*
* @param int $days Nombre de jours
* @return bool
*/
public function isRecent(int $days = 7): bool
{
$limit = new DateTime("-{$days} days");
return $this->createdAt > $limit;
}
/**
* Retourne les données prêtes à persister en DB.
*