Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

heropaulexy's avatar

PHP ERROR

I am coding a simple product crud application. I have a little issue, when i fill in the form and hit the create new product button, it goes to the database but the route returns PAGE NOT FOUND instead of redirecting to the products page.

the router.php file

<?php

namespace app;

class router
{
    public array $getRoutes = [];
    public array $postRoutes = [];
    public Database $db ;

    public function __construct(){
        $this -> db = new Database();
    }

    public function get($url, $fn){
        $this -> getRoutes[$url] = $fn;
    }
    public  function post($url, $fn){
        $this -> postRoutes[$url] = $fn;
    }
    public function resolve(){
        $currentUrl = $_SERVER['PATH_INFO'] ?? '/';
        $method = $_SERVER['REQUEST_METHOD'];

        if ($method === 'GET'){
            $fn = $this->getRoutes[$currentUrl] ?? null;
        }
        else{
            $fn = $this->postRoutes[$currentUrl] ?? null;
        }
        if ($fn){
            call_user_func($fn, $this);
        }
        else{
            echo "page not found";
        }
    }
    public function renderViews($view, $params = [])
    {
        foreach ($params as $key => $value){
            $$key = $value;
        }
        ob_start();
        include_once __DIR__."/views/$view.php";
        $content = ob_get_clean();
        include_once __DIR__."/views/_layout.php";
    }
}

here's the controller for it

public function create(Router $router)
    {
        $errors = [];
        $productData = [
            'title' => '',
            'description' => '',
            'image' => '',
            'price' => '',
        ];
        if ($_SERVER['REQUEST_METHOD'] === 'POST'){
            $productData['title'] = $_POST['title'];
            $productData['description'] = $_POST['desc'];
            $productData['imageFile'] = $_FILES['image'] ?? null;
            $productData['price'] = (float)$_POST['price'];

            $product = new products();
            $product->load($productData);
            $errors = $product -> save();
            if (empty($errors)){
                header('Location: /products');
                exit();
            }
        }
0 likes
12 replies
Sinnbeck's avatar

So what you are saying is that you are going inside the resolve() metjod and hit this line?

            echo "page not found";

Did you try debugging what $currentUrl is there?

heropaulexy's avatar

@Sinnbeck Yes, it is hitting the echo "page not found" line though it is populating the database

Sinnbeck's avatar

@heropaulexy did you add the code before going to the create route? Add it perhaps inside the get check just before submitting

Sinnbeck's avatar

@heropaulexy you can use var dump to inspect it if needed

echo "page not found";
var_dump($this->getRoutes); die;
Snapey's avatar

Do you have something that can reply to /products ?

You say the data is saved, so we can assume that this

            $errors = $product -> save();
            if (empty($errors)){
                header('Location: /products');
                exit();
            }

results in a redirection to the /products route.

When you dump and die in the router, it stops on the first iteration (when saving the record) and not on the second iteration (when trying to resolve /products)

Please or to participate in this conversation.