skip to Main Content

I created a router from scratch. It’s a simple router :

class Router {
    private static array $routes;

    public static function register($route = '/', $filename=''){
        self::$routes[$route] = $filename;
    }

    public static function run(string $URI){
        $route = explode('?', $URI)[0];
        $play = self::$routes[$route];

        if($play){
            try{
                var_dump($play);
                require_once $play;
            } catch(Error $e){
                echo "<h1>Can't find the file </h1> <p>" . $e -> getMessage() . "</p>";
            }
            
        }else{
            echo "<h1>Page not found 404</h1>";
        }
    }
}

And the main page index.php is using a header and a footer as external files. In the middle, we can call the router and it just works. The logic is clear :

require_once dirname(__DIR__) . '/config/Router.php';
/**
 * Registration of routers =======================+
 */
Router::register('/', './index.php');
Router::register('/associations', dirname(__DIR__) . '/views/associations/index.php');
Router::register('/associations/add', dirname(__DIR__) . '/views/associations/add.php');
Router::register('/partnerships', dirname(__DIR__) . '/views/partnerships/index.php');
Router::register('/test/t', dirname(__DIR__) . '/views/associations/test.php');
/**
 * Definitions of variables ======================+
 */
$title = 'List of associations';
$page_title = 'Organize your partnerships in ***';
/**
 * Requiring the header for every page ===========+
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . '/views/layouts/header.php';
/**
 * 
 */
var_dump($_SERVER['REQUEST_URI']);
Router::run($_SERVER['REQUEST_URI']);

/**
 * Requiring the header for every page ===========+
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'views/layouts/footer.php';

For the pages that have just one slash, it works perfectly. But with pages like /associations/add, the page is uploaded correctly but it can’t read the CSS.

NOTE : I am not using composer and I did not make an autoloader !

I tried to change the path to CSS files but it will affects the other files to because they all have the same header. And this is not the problem. I am sure that the routing is the problem. But how can I fix this? Any help would be appreciated !

2

Answers


  1. could you give example of you html template?

    I guess, you use a relative path:

    <link rel="stylesheet" type="style.css">
    

    on the page /associations/add it will try to read not from /style.css but from /associations/style.css and it’s causing your issue.

    You should use an absolute path:

    <link rel="stylesheet" type="text/css" href="/style.css">
    
    Login or Signup to reply.
  2. Do you know, that dirname(__DIR__) reduces the directory DIR by the last component?

    Example: __DIR__ (the current directory) is /a/b/c/d. Then dirname(__DIR__) returns /a/b/c.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search