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

Dast3an's avatar

my route didn't find about page it works only root path

i have the below code , i did what jefery did in php for bigenner but i stuck from routing please any one tell me what's the problem

nav.php

                     <div class="ml-10 flex items-baseline space-x-4">
                            <a href = "/" class="<?= urlIs('/') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-white">Home</a>
                            <a href = "/about" class="<?= urlIs('/about') ? 'bg-gray-900 text-gray-300' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">About</a>
                            <a href = "/contact" class="<?=  urlIs('/contact') ? 'bg-gray-900 text-gray-300' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Contact</a>
                        </div>

index.php

require 'functions.php';

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];

$routes = [ '/' => 'controllers/index.php', '/about' => 'controllers/about.php', '/contact' => 'controllers/contact.php', ]; if (array_key_exists($uri, $routes)) { require $routes[$uri]; } else { echo "Route not found for URL: "; }

function.php

function urlIs($value) { return $_SERVER['REQUEST_URI'] === $value; }

0 likes
3 replies
LaryAI's avatar
Level 58

It looks like you're trying to implement a simple routing mechanism in PHP, but there are a few issues in your code that might be causing the problem. Let's go through them step by step.

  1. Ensure the URLs are correctly matched: The urlIs function should be checking the path part of the URL, not the entire URL. Also, make sure the paths in your $routes array match exactly what you expect in the $_SERVER['REQUEST_URI'].

  2. Check for trailing slashes: Sometimes, URLs with and without trailing slashes are treated differently. Ensure consistency in your routing logic.

  3. Debugging: Add some debugging statements to see what the actual values are.

Here's a revised version of your code:

nav.php

<div class="ml-10 flex items-baseline space-x-4">
    <a href="/" class="<?= urlIs('/') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-white">Home</a>
    <a href="/about" class="<?= urlIs('/about') ? 'bg-gray-900 text-gray-300' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">About</a>
    <a href="/contact" class="<?= urlIs('/contact') ? 'bg-gray-900 text-gray-300' : 'text-gray-300' ?> px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Contact</a>
</div>

index.php

<?php
require 'functions.php';

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$routes = [
    '/' => 'controllers/index.php',
    '/about' => 'controllers/about.php',
    '/contact' => 'controllers/contact.php',
];

if (array_key_exists($uri, $routes)) {
    require $routes[$uri];
} else {
    echo "Route not found for URL: " . htmlspecialchars($uri);
}
?>

functions.php

<?php
function urlIs($value)
{
    return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === $value;
}
?>

Explanation:

  1. nav.php: The urlIs function is used to determine the active link. The class names are applied based on the current URL.
  2. index.php: The parse_url function is used to get the path part of the URL. This ensures that query parameters do not interfere with routing.
  3. functions.php: The urlIs function is updated to use parse_url to compare only the path part of the URL.

This should resolve the issue with your routing. If you still encounter problems, make sure to check the actual values of $_SERVER['REQUEST_URI'] and $uri by adding some var_dump or echo statements for debugging.

Tray2's avatar
Tray2
Best Answer
Level 73

Sounds like a webserver issue, what are you using to serve the pages?

If you are using MacOs or Windows, then I suggest using Laravel Herd as you dev environment, If you are using Linux, I suggest giving Valet for linux a try.

Dast3an's avatar

perfect i change my xampp to laravel herd , and it works like a charm

Please or to participate in this conversation.