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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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();
}
}
Please or to participate in this conversation.