0

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 !

3 Answers 3

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">
1
  • Well, I tried that. <link rel="stylesheet" href="/public/headerStyle.css">. Now, even the pages that were linking the css aren't anymore. Commented Jul 6 at 10:27
0

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.

1
  • Why? Has DIR anything to do with the URI? It just works on the actual files. This is what I thought. Commented Jul 6 at 10:22
0

Okay guys thank you all. It is solved. As @ruslan said, I used absolute paths. The problem is in fact in the uri. I still don't understand why and how but it works now. In the public index.php I define first of all the constant (or just a variable) :

define('ABSOLUTE_PATH', dirname(__FILE__) . '/../');

That's it !

Not the answer you're looking for? Browse other questions tagged or ask your own question.