I’m on a localhost server. Only the index page for my router is showing, but when I try to go to the about.php and contact.php pages I made, this error message appears ‘The requested resource /contact.php was not found on this server.’. I’m supposed to get this error message though if my route doesn’t work ‘throw new Exception(‘No route defined for this URI.’);’. I’ve been following a tutorial to make this and I’m not sure how to debug this.
This is my index.php file
<?php
require 'core/bootstrap.php';
require Router::load('routes.php')
->direct(Request::uri());
It’s supposed to load the router and direct it to the correct php file.
here is a router object with links. Only the ‘’ => controllers/index.php link is working correctly.
<?php
$router->define([
'' => 'controllers/index.php',
'about' => 'controllers/about.php',
'contact' => 'controllers/contact.php'
]);
This is a Router class to load the router object and use the links from it to go to the correct file.
<?php
class Router
{
protected $routes = [];
public static function load($file)
{
$router = new static;
require $file;
return $router;
}
public function define($routes)
{
$this->routes = $routes;
}
public function direct($uri)
{
if (array_key_exists($uri, $this->routes)) {
return $this->routes[$uri];
}
throw new Exception('No route defined for this URI.');
}
}
This class takes the request_uri from the page and uses it to get to the correct file with the router class.
<?php
class Request
{
public static function uri()
{
return trim($_SERVER['REQUEST_URI'], '/');
}
}
And this file takes everything connects all the files together.
<?php
$app = [];
$app['config'] = require 'config.php';
require 'core/Router.php';
require 'core/Request.php';
require 'core/database/connection.php';
require 'core/database/queryBuilder.php';
$app['database'] = new QueryBuilder(
Connection::make($app['config']['database'])
);
I’d appreciate any help with this. Here is a link to the project on github https://github.com/aioy/firstBlog