Router is only showing index page and other routes are not working

Router is only showing index page and other routes are not working
0

#1

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


#2

I found the error. In my views file I had to change my tag href link from ‘about.php’ to just ‘/about’, and now its working.


#3

class NestController extends BaseController {

public function showView()
{
    return View::make('nest');
}

}
Route:

Route::get(’/nest’, ‘[email protected]’);
When I go to url/nest it doesnt work. When I go to url/index.php/nest it does work.

Obviously I just want it to be /nest without the index.php.
you can try it, if you face this problem.

If you are using Belkin router and face any problem with this router visit here
belkin router Support


#4

I can’t really provide general support for things so far outside the content of the course, but one quick thing that might get you pointed in the right direction For more contact HP Support