PHP MVC Framework Issue

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:

  1. Is this even the proper basic structure of a php mvc framework as far as the folders and file structures go?

  2. 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.

Yeah the index.php doesn’t look right at all. First of all it should be the entry point for all requests and so doesn’t make sense to be sending the user just to the books controller. It should be looking at the URL, parsing it, deciding which controller needs to be called, include that controller and call on it. This is usually done in the index.php file itself and not using something like a header location redirect.

1 Like

Thank you for your helpful reply. So, could you show me a specific example of the index.php relating to the other example files I’ve created with ChatGPT? Thanks.

What should go in the index.php file?

Welcome to the Forum.

Here is the real problem. You do not know Php well enough to ask GPT the right questions. The code response it gave you shows that. Second problem is based on the first. You dont know Php well enough to know when GPT gives you bad or less than optimum code and it absolutley will based on the first problem of not knowing what to ask it for and in this case, that is exactly what it did.

If you already know what your doing, GPT can be a great tool to speed up your process. Depending on GPT to write cut an paste working code when you don’t know what to ask for is not going to teach you anything. Furthermore, asking us to help debug the code that GPT spit out is out of place on this forum. We are here to happily help you with code you wrote and are stuck on, not debug a poor GPT code response. I would suggest finding some current tutorials that use PDO and prepared statements and start from there. If you have a question about what a piece of code does or means, GPT can be of great help, but you are not ready to ask it to be writing the code for you.

If you are really interested in MVC Frameworks, check out how existing ones are structured. Their code is readily available on GitHub.

PS: Don’t be surprised if you get hammered on your duplicate post on Stack Overflow. They can be brutal over there.

If your desire is to learn, here is a good tutorial to start with.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.