skip to Main Content

Mini Notes App has two pages, Homepage and About page. While the Home page works, the about page does not. It displays the message: NOT FOUND. The requested URL was not found.
This is the ‘controllers/index.php’ file.

<?php

$heading = 'Home';

require 'views/index.view.php'; 

This is the corresponding ‘views/index.view.php’ file

<?php require('partials/head.php') ?>
  
<?php require('partials/nav.php') ?>
<?php require('partials/banner.php') ?>


  <main>
       <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
          Welcome to the Zanga
         </div>`
     </main>

 <?php require('partials/footer.php') ?>

This is the ‘index.php’ file

require 'functions.php';
<?php

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
    '/' => 'controllers/index.php',
    '/about' => 'controllers/about.php',
];

function routeToController($uri, $routes) {
    if (array_key_exists($uri, $routes)) {
        require $routes[$uri];
    } else {
        abort();
    }

}


function abort($code = 404) {
    http_response_code($code);

    require "views/{$code}.php";

    die();
}

routeToController($uri, $routes);

This is the ‘views/about.view.php

<?php require('partials/head.php') ?>

<?php require('partials/nav.php') ?>
<?php require('partials/banner.php') ?>
  
 <main>
     <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
         You are on the about page
         </div>
     </main>

<?php require('partials/footer.php') ?>

I thoroughly analysed routes for any bugs but all in vain. ‘controllers/about.php’ always displays ‘Not Found. Requested URL not found.

I tried to duplicate the website but it still displays . Not Found. The requested URL was not found in chrome.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to resolve the issue '404 NOT FOUND. The requested url was not found' by setting up URL re-writing using Apache and a '.htaccess' file in `the root directory. It allowed the web server to redirect requests for specific URLs to the appropriate PHP file. Thus;

    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    I created '.htaccess' file in the root directory of the project and used the code above and saved it. The 'controllers/about.php' is now working. The code enabled URL rewriting in Apache, and made it to redirect allrequests that are not for existing files or directories to index.php , with the original URL appended as a parameter.


  2. Before doing anything else, could you please test the code by running a local PHP server?

    Open a terminal at the root of your project and type the following command:

    php -S localhost:8000
    

    Try navigating to it. If it doesn’t work, take a look at the code below that I have recreated.

    // controllers/index.php
    <?php
    
    $heading = 'Home';
    
    require './views/index.view.php'; 
    
    <?php
    // controllers/about.php
    $heading = 'About';
    
    require 'views/about.view.php'; 
    
    // views/index.view.php 
    <?php require('partials/head.php') ?>
    
    <?php require('partials/nav.php') ?>
    <?php require('partials/banner.php') ?>
      
     <main>
         <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
             Welcome to the Zanga
             </div>
         </main>
    
    <?php require('partials/footer.php') ?>
    
    // views/about.view.php 
    <?php require('partials/head.php') ?>
    
    <?php require('partials/nav.php') ?>
    <?php require('partials/banner.php') ?>
      
         <main>
             <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
                You are on the about page
             </div>
         </main>
    
    <?php require('partials/footer.php') ?>
    
    <?php
    // index.php
    $uri = parse_url($_SERVER['REQUEST_URI'])['path'];
    $routes = [
        '/' => 'controllers/index.php',
        '/about' => 'controllers/about.php',
    ];
    
    function routeToController($uri, $routes) {
        if (array_key_exists($uri, $routes)) {
            require $routes[$uri];
        } else {
            abort();
        }
    
    }
    
    
    function abort($code = 404) {
        http_response_code($code);
    
        require "views/{$code}.php";
    
        die();
    }
    
    routeToController($uri, $routes);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search