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

vk_monitor's avatar

PHP Practitioner: Creating a router (with no framework) - lesson 16

Hi, I am currently going through the PHP Practitioner course and I'm looking at the routers lesson. I have followed the tutorial and verified the code I've created is the same as that displayed on the GitHub account. I'm still facing the same error code: "Not Found. The requested URL was not found on this server."

When I clone the repo from GitHub I get the same issue... I'm not too sure why this issue is occurring. I feel it's something to do with the code: $uri = trim($_SERVER['REQUEST_URI'],'/');

Would be grateful to get a hand with this.

Thanks! :)

0 likes
26 replies
jlrdw's avatar

What uri are you sending?

What file is $uri = trim($_SERVER['REQUEST_URI'],'/'); in?

vk_monitor's avatar

I'm using XAMPP and the uri I'm using is:

http://localhost/laracasts

The routes.php file I've created has the following routes defined:

<?php 


$router->define([
    ""=> 'controllers/index.php',
    "about" => 'controllers/about.php',
   "about/culture" => 'controllers/about-culture.php',
    "contact" => 'controllers/contact.php'
]);
tykus's avatar

@Vaishk check what the URI is. In the Request class, var_dump and die:

var_dump($_SERVER['REQUEST_URI']);die;

Visit one of the pages and see what is dumped. If it includes the laracasts segment; then change Request class:

<?php

class Request
{
    public static function uri()
    {
        return trim(str_replace('laracasts', '', $_SERVER['REQUEST_URI'], '/');
    }
}
vk_monitor's avatar

@tykus Hi Tykus - I tried this and whenever I go to the url: localhost/laracasts/about I get the error message: NOT FOUND - the requested URL was not found on this server.

All of the pages are defined in the controllers and the views folders.

tykus's avatar

@Vaishk it looks like the web server is not serving your request rather than a 404 coming from the application code - I don't use WAMP/XAMPP so I don't know if/how you configured it. Try running PHP's own built-in server instead - inside the application directory, run:

php -S localhost:8000

and visiting http://localhost:8000/about

vk_monitor's avatar

@tykus Thanks - just tried this and even on php running on it's own built-in server is returning a 404 code. I do think this is to do with the project directory...

tykus's avatar

@Vaishk are you getting the exception message from the Router class in the repo; or something else?

vk_monitor's avatar

@tykus hmm, I'm not too sure actually. Any chance you're free in 45 mins and I can screen share via a google meet link?

vk_monitor's avatar

I just think it might be easier to show you the error and then I can post the full answer on the chain so that it can help others who are using XAMPP

tykus's avatar

@vk_monitor so XAMPP (Apache) doesn't like the URL...

You probably need a RewriteRule in the Apache config. Are you using XAMPP for other projects or just this one?

tykus's avatar

@vk_monitor if you click on the Explorer button in the XAMPP GUI; you will open an Explorer window in the XAMPP directory; browse through apache/conf/extra and open the http-vhosts.conf file. In there, add a VirtualHost block:

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/laracasts" # make sure path is correct
    RewriteEngine On
    RewriteRule ^(.*)$ /index.php/
</VirtualHost>

Restart Apache in the XAMPP GUI; you should be able to use http://localhost/about now.

Be aware this is the minimum viable solution to serve localhost from the laracasts directory only - if you were serving other projects as http://localhost/{PROJECT_NAME} they will break Check out this StackOverflow thread for discussion on implementing different "domains" using the hosts file.

1 like
vk_monitor's avatar

@tykus Thanks Tykus, that's certainly helped remove the error page. I'm still getting an issue that when I type in the URL http://localhost/laracasts/about I now get a blank page.

What does a virtual host actually mean? I'm asking this to understand if my code where I set the var $uri now needs updating.

<?php 

$query = require 'core/bootstrap.php';



$router = new Router;

require 'routes.php';


$uri = trim(str_replace('laracasts', '', $_SERVER['REQUEST_URI']), '/');


$router =  Router::load('routes.php');

$router->direct(Request::uri());
tykus's avatar

@vk_monitor the $uri assignment should revert to the original version:

$uri = trim($_SERVER['REQUEST_URI']), '/');

And you should be visiting http://localhost/about - i.e. no "laracasts" anymore.

vk_monitor's avatar

@tykus I thought that might be the case, however, when I now try and go to the URL http://localhost/laracasts/ I get the error code:

Fatal error: Uncaught Exception: no routes defined for this uri in C:\xampp\htdocs\laracasts\core\Router.php:32 Stack trace: #0 C:\xampp\htdocs\laracasts\index.php(17): Router->direct('laracasts') #1 {main} thrown in C:\xampp\htdocs\laracasts\core\Router.php on line 32

That doesn't align with the virtualhost block that was applied... I feel like we're getting closer.

When I make the routes (see below) then I no longer get the error but I end up getting a blank page instead.


<?php 


$router->define([
    "laracasts"=> 'controllers/index.php',
    "laracasts/about" => 'controllers/about.php',
    "laracasts/about/culture" => 'controllers/about-culture.php',
    "laracasts/contact" => 'controllers/contact.php'
]);

tykus's avatar

@vk_monitor Why is there a Laracasts segment in the defined routes? Revert that code to the tutorial example.

vk_monitor's avatar

@tykus So when I change the routes back to this:

<?php 


$router->define([
    ""=> 'controllers/index.php',
    "about" => 'controllers/about.php',
    "about/culture" => 'controllers/about-culture.php',
    "contact" => 'controllers/contact.php'
]);

and I go to the URL http://localhost/laracasts/ then I get the following error code:

Fatal error: Uncaught Exception: no routes defined for this uri in C:\xampp\htdocs\laracasts\core\Router.php:32 Stack trace: #0 C:\xampp\htdocs\laracasts\index.php(17): Router->direct('laracasts') #1 {main} thrown in C:\xampp\htdocs\laracasts\core\Router.php on line 32

Additionally, as you mentioned this is the httpd-vhosts.conf file code (same as what you sent)

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/laracasts"
    RewriteEngine On
    RewriteRule ^(.*)$ /index.php/
</VirtualHost>
tykus's avatar

@vk_monitor I did mention earlier....

And you should be visiting http://localhost/about - i.e. no "laracasts" anymore.

At least you are getting to the Router class' Exception!

Also, make sure you restart the Apache service whenever changing config.

1 like
vk_monitor's avatar

@tykus ok, ahh - I missed that. Right, that makes sense. The only issue I am facing now (apologies, new to php and so trying to debug things is very hard as I am not too sure how to).

this is my index.view.php page:

<h1>My Tasks</h1>

<?php foreach ($tasks as $task) : ?>
    <li>
        <?php if ($task->completed) : ?>
            <strike><?= $task->description; ?></strike>
        <?php else : ?>
            <?= $task->description; ?>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

and this is my routes:

<?php 


$router->define([
    ""=> 'controllers/index.php',
    "about" => 'controllers/about.php',
    "about/culture" => 'controllers/about-culture.php',
    "contact" => 'controllers/contact.php'
]);

and this is the controllers/index.php:

<?php 

$tasks = $app['database']->selectAll('todos', 'Task');
require'views/index.view.php';

yet when I go to the url: http://localhost/ I get a blank page. I.e. none of my code is rendering.

//This is my router class (just incase I am making an error here that I can't spot)

<?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 routes defined for this uri');

    }


}

tykus's avatar

@vk_monitor the tools you choose can greatly affect your experience.

Are any of the defined Routes working?

vk_monitor's avatar

@tykus Nope, none of the routes seem to be working. All just return a blank screen :(

vk_monitor's avatar

I'm not even sure how to debug this, as the front end isn't showing any error codes. But I'm adamant that I resolve this bug before continuing.

jlrdw's avatar

@vk_monitor in your xampp you should be able to have this in address bar:

http://localhost/laracasts/about

Make sure your httpd.conf file is setup correct.

vk_monitor's avatar

Hi All, I've been really stuck on this tutorial for a while now. After a lot of googling and asking those who are experts in the field I have found this solution to work:

  • create a file called .htaccess
  • within that file add the code below
  • please note that I am using XAMPP and within the htdocs folder I have a folder called lessons where all the code is stored for lesson 16
RewriteEngine On 
RewriteBase /lessons/ 
RewriteRule ^.*$ index.php [END]

what this does is that it creates a rewrite module so that whenever you input in localhost/lessons/about it reroutes it to index.php. Index.php has a router which directs the code as to where to direct that additional url to. Please notes that my routes file is a little different and is this code:

<?php 


$router->define([

    "lessons" => "controllers/index.php",
    "lessons/about" => "controllers/about.php",
    "lessons/contact" => "controllers/contact.php"

]);

?>

I hope this helps someone :)

Please or to participate in this conversation.