Undefined variable incompréhensible en MVC et POO
Bonjour à tous,
Je suis en train de suivre le cours sur l'architecture MVC en PHP.
Tout allait bien jusqu'à devoir créer un routeur...
Avant tout, voici les codes nécessaires:
index.php (le routeur):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?php
require('controller/controller.php');
try {
if(isset($_GET['action'])) {
if ($_GET['action'] == 'listPosts') {
listPosts();
}
elseif ($_GET['action'] == 'post') {
if (isset($_GET['id']) && $_GET['id'] > 0) {
post();
}
else {
throw new Exception('Aucun identifiant de billet envoyé');
}
}
elseif ($_GET['action'] == 'addComment') {
if (isset($_GET['id']) && $_GET['id'] > 0) {
if (!empty($_POST['author']) && !empty($_POST['comment'])) {
addComment($_GET['id'], $_POST['author'], $_POST['comment']);
}
else {
throw new Exception('tous les champs ne sont pas remplis');
}
}
else {
throw new Exception('Aucun identifiant de billet envoyé');
}
}
}
else {
listPosts();
}
} catch (Exception $e) {
echo 'Erreur: ' . $e->getMessage();
} |
controller.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php
//Chargement des classes
require_once('model/postManager.php');
require_once('model/CommentManager.php');
function listPosts()
{
$postManager = new PostManager();
$posts = $postManager->getPosts();
require('view/listPostsView.php');
}
function post()
{
$postManager = new PostManager();
$commentManager = new CommentManager();
$post = $postManager->getPost($_GET['id']);
$comments = $commentManager->getComments($_GET['id']);
require('view/viewPost.php');
}
function addComment($postId, $author, $comment)
{
$commentManager = new CommentManager();
$affectedLines = $commentManager->postComments($postId, $author, $comment);
if ($affectedLines === false) {
throw new Exception('Impossible d\'ajouter le commentaire');
}
else {
header('Location: index.php?action=post&id=' . $postId);
}
} |
postManager.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?php
/**
*
*/
class PostManager
{
public function getPosts()
{
$db = $this->dbConnect();
$dbreq = $db->query('SELECT id, titre, contenu, DATE_FORMAT(date_creation, \'%d/%m/%Y à %Hh%imins%ss\') AS creation_date FROM billets ORDER BY date_creation DESC LIMIT 0, 5');
return $dbreq;
}
public function getPost($idPost)
{
$db = $this->connectDb();
$reqpost = $db->prepare('SELECT id, titre, contenu, DATE_FORMAT(date_creation, \'%d/%m/%Y à %Hh%imins%ss\') AS creation_date FROM billets WHERE id=?');
$reqpost->execute(array($idPost));
$post = $reqpost->fetch();
return $post;
}
private function dbConnect()
{
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
return $db;
}
} |
CommentManager.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?php
/**
*
*/
class CommentManager
{
public function getComments($idPost)
{
$db = $this->dbConnect();
$comments = $db->prepare('SELECT id, auteur, commentaire, DATE_FORMAT(date_commentaire, \'%d/%m/%Y à %Hh%imins%ss\') AS creation_date_fr FROM commentaires WHERE id_billet=? ORDER BY date_commentaire DESC');
$comments->execute(array($idPost));
return $comments;
}
public function postComments($postId, $postAuthor, $postComment)
{
$db = $this->dbConnect();
$reqPostComments = $db->prepare('INSERT INTO commentaires(id_billet, auteur, commentaire, date_commentaire) VALUES (?, ?, ?, NOW())');
$affectedlines = $reqPostComments->execute(array($postId, $postAuthor, $postComment));
return $affectedlines;
}
private function dbConnect()
{
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
return $db;
}
} |
template.php:
Code:
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?= $title ?></title>
<link rel="stylesheet" type="text/css" href="public/css/style.css" />
</head>
<body>
<?= $content ?>
</body>
</html> |
listPostView.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php $title = 'Mon super blog'; ?>
<?php ob_start(); ?>
<p>Derniers billets</p>
<?php
while($data = $posts->fetch())
{
?>
<div class="news">
<h3><?= $data['titre']; ?><em><?= $data['creation_date']; ?></em></h3>
<p><?= $data['contenu']; ?><br />
<a href="view/viewPost.php?id=<?= $data['id']; ?>">Commentaires</a></p>
</div>
<?php
}
$posts->closeCursor();
?>
<?php $content = ob_get_clean(); ?>
<?php require('template.php'); ?> |
viewPost.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?php $title = 'Commentaires'; ?>
<?php ob_start(); ?>
<h1>Mon super blog!</h1>
<p><a href="../index.php">Retourner à la liste des billets</a></p>
<div class="news">
<h3><?php echo $post['titre']; ?><em><?php echo $post['creation_date']; ?></em></h3>
<p><?= $post['contenu']; ?></p>
</div>
<h2>Commentaires</h2>
<form action="index.php?action=addComment&id=<?= $post['id'];?>" method="post">
<div>
<label for="author">Auteur</label><br />
<input type="text" id="author" name="author" />
</div>
<div>
<label for="comment">Commentaire</label><br />
<textarea id="comment" name="comment"></textarea>
</div>
<div>
<input type="submit" />
</div>
<?php
while ($comment = $comments->fetch()) {
?>
<strong><?= $comment['auteur']; ?></strong> le <?= $comment['creation_date_fr']; ?></strong>
<p><?= $comment['commentaire'] ?></p>
<?php
}
?>
<?php $content = ob_get_clean(); ?>
<?php require('template.php'); ?> |
Et alors que tout fonctionne pour la page listPostsView.php, lorsque je clique sur le lien Commentaires pour afficher les commentaires liés au post ou pour pouvoir le commenter, j'ai les erreurs suivantes qui s'affichent:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Notice: Undefined variable: post in E:\wamp\www\php\mysql\TP blog\view\viewPost.php on line 8
Call Stack
# Time Memory Function Location
1 0.0013 242720 {main}( ) ...\viewPost.php:0
( ! ) Notice: Undefined variable: post in E:\wamp\www\php\mysql\TP blog\view\viewPost.php on line 8
Call Stack
# Time Memory Function Location
1 0.0013 242720 {main}( ) ...\viewPost.php:0
( ! ) Notice: Undefined variable: post in E:\wamp\www\php\mysql\TP blog\view\viewPost.php on line 9
Call Stack
# Time Memory Function Location
1 0.0013 242720 {main}( ) ...\viewPost.php:0
Notice: Undefined variable: comments in E:\wamp\www\php\mysql\TP blog\view\viewPost.php on line 31
Call Stack
# Time Memory Function Location
1 0.0013 242720 {main}( ) ...\viewPost.php:0
( ! ) Fatal error: Call to a member function fetch() on null in E:\wamp\www\php\mysql\TP blog\view\viewPost.php on line 31
Call Stack
# Time Memory Function Location
1 0.0013 242720 {main}( ) ...\viewPost.php:0 |
Cela fait 3 jours que j'inspecte mon code, et je ne trouve toujours pas le problème...
Merci d'avance pour votre aide :)