Cleaned
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user