0

Hello I have an issue with my view fieldOverview.php I have a view fields.php which has link to other view

<a href="/fieldOverview/<?= $field->getId(); ?>" class="field-options" >
                        Past actions and notes
</a>

when user clicks on this link he is correctly redirected but the css doesn't load

however when i will go to http://localhost:8080/fieldOverview instead of http://localhost:8080/fieldOverview/IDNUMBER

the css styles works properly. Even though both times it goes through this function

public function fieldOverview($id){
    //$field = $this->fieldRepository->findFieldById($id);
    return $this->render('fieldOverview');
}

my render function

protected function render(string $template = null, array $variables=[]){
    $templatePath = 'public/views/'. $template.'.php';
    $output = 'File not found';

    if (file_exists($templatePath)){
        extract($variables);

        ob_start();
        include $templatePath;
        $output = ob_get_clean();
    }

    print $output;
}

maybe to make it more clear this is my index.php

require 'Routing.php';
$path = trim($_SERVER['REQUEST_URI'], '/');
$path = parse_url($path, PHP_URL_PATH);

Router::get('', 'DefaultController');
Router::get('fields', 'FieldController');
Router::get('fieldOverview', 'FieldController');

Router::run($path);

and my Routing.php

class Router {

    public static $routes;
    private static $controller;

    public static function get($url, $view) {
        self::$routes[$url] = $view;
    }

    public static function post($url, $view) {
        self::$routes[$url] = $view;
    }

    public static function run ($url) {

        $urlParts = explode("/", $url);
        $action = $urlParts[0];

        if (!array_key_exists($action, self::$routes)) {
            die("Wrong url!");
        }

        if(session_status() !== PHP_SESSION_ACTIVE){
            session_start();
        }

        if ((!isset($_COOKIE['user']) || !isset($_SESSION['logged_in_user_account_id'])
                || !isset($_SESSION['logged_in_personal_data_id']))
            && ( $action !== 'signUp' && $action !== 'login')){
            self::$controller = self::$routes['login'];
            $action = 'login';
        }else{
            self::$controller = self::$routes[$action];
        }

        $object = new self::$controller;
        $action = $action ?: 'index';

        if (isset($urlParts[1])){
            $id = intval($urlParts[1]);
        }else{
            $id ='';
        }

        $object->$action($id);
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
hardy6413
  • 11
  • 2
  • 1
    How are you loading the CSS? Are you using relative path `href="path/to/css"` or absolute from the document root: `href="/path/to/css"` (notice the difference with the first `/`)? If it's the first (relative), change it to absolute from the document root. When you use relative paths, the browser will use the current URL as base, so on page `/foo`, it will load from `/foo/style.css` and on `/foo/bar`, it will try and load it from `/foo/bar/style.css`. Using absolute from path (starting with `/`) will make it load from the same URL everywhere. – M. Eriksson Jan 14 '22 at 17:02
  • 1
    We don't need to see more PHP. The issue is most likely how your HTML is referencing and the client is loading the CSS file (your ` – M. Eriksson Jan 14 '22 at 17:08
  • wow thank you, that was excatly mi mistake ! so happy than i know the solution finally – hardy6413 Jan 14 '22 at 17:10
  • Does this answer your question? [relative path to CSS file](https://stackoverflow.com/questions/17621324/relative-path-to-css-file) - Even if the question is closed, the most upvoted answer explains this issue very well. – M. Eriksson Jan 14 '22 at 17:12
  • _For future reference_ Wrap code with 3 backticks `\`\`\`` not 3 single quotes – RiggsFolly Jan 14 '22 at 17:16
  • yes it does i didnt have idea it works like that tbh – hardy6413 Jan 14 '22 at 17:16

0 Answers0