Learning the PHP MVC framework is entirely new to me. I’ve used ChatGPT to show me a basic PHP MVC folder & file structure.
I have a demo website that will use my own MVC framework that has a books gallery on the homepage. When the user clicks on a specific book from the list of books in a gallery on the homepage, the books details page will load only that specific book to show it’s details.
Although ChatGPT seems to have gotten majority of the folder and file structure correct, it’s still not properly loading the details page. I keep getting the following error after clicking on a specific book on the homepage:
“Not Found
The requested URL was not found on this server.”
Here’s how my folders are structured:
index.php
Models
- Books.php
Views
- home.php
- details.php
controllers
- BooksController.php
index.php file
<?php
header("Location: controllers/BooksController.php");
exit;
?>
Books.php
<?php
class Books {
private $conn;
public function __construct($conn) {
$this->conn = $conn;
}
#homepage/default page function:
public function getAllBooks() {
$query = "SELECT * FROM book_details";
$stmt = $this->conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
#details page when user clicks on a specific book for details of book:
public function getBookById($id) {
$query = "SELECT * FROM book_details WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
}
?>
home.php
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>List of Books</h1>
<ul>
<?php foreach ($books as $book) { ?>
<li>
<a href="details.php?action=details&id=<?php echo $book['id']; ?>">
<?php echo $book['name']; ?>
</a>
</li>
<?php } ?>
</ul>
</body>
</html>
BooksController.php
<?php
require_once('../models/Books.php');
// Create a database connection
$conn = new mysqli("localhost", "username", "password", "booksdb");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$booksModel = new Books($conn);
if(isset($_GET['action']) && $_GET['action'] == 'details') {
if(isset($_GET['id'])) {
$book = $booksModel->getBookById($_GET['id']);
include('../views/details.php');
} else {
// Handle if no ID is provided for details view
echo "Invalid ID";
}
} else {
$books = $booksModel->getAllBooks();
include('../views/home.php');
}
$conn->close();
?>
details.php
<!DOCTYPE html>
<html>
<head>
<title>Book Details</title>
</head>
<body>
<h1>Book Details</h1>
<?php if($Book) { ?>
<h2><?php echo $Book['name']; ?></h2>
<p>Description: <?php echo $Book['desc']; ?></p>
<p>Category: <?php echo $Book['category']; ?></p>
<?php } else { ?>
<p>Book not found!</p>
<?php } ?>
</body>
</html>
So, my questions are:
-
Is this even the proper basic structure of a php mvc framework as far as the folders and file structures go?
-
What exactly is wrong with the way that the index.php and details.php files are interacting? The details page never loads a specific book from the homepage. What exactly should be in the index.php file?
Any support is appreciated.
*I tried the code above, expected the details page to load the details of a book from the homepage, but it did not properly load a details page.