【问题标题】:css style not applied because of a parameter passed into the url由于传入 url 的参数,css 样式未应用
【发布时间】:2021-03-25 21:48:33
【问题描述】:

我正在尝试制作自己的 mvc,但我的 css 路径出现错误。当我在主页上时,一切正常,但由于我导航到其他页面,css 路径更改为 mywebsite/articles/css/style.css,我不知道如何解决这个问题如果有人可以帮助我。提前谢谢你,这是我的代码:

index.php

<?php


define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']));

require_once ROOT. 'models/Model.php';
require_once ROOT.'models/Controller.php';


$url = explode('/', $_GET['url']);

if($url[0] !== ''){
    if(file_exists('controllers/'.ucfirst($url[0]).'.php')){
        $controller = ucfirst($url[0]);
        $action = isset($url[1]) ? $url[1] : 'index'; 
        require_once ROOT.'controllers/'.$controller.'.php';
        $controller = new $controller();
        if(method_exists($controller, $action)){
            unset($url[0]);
            unset($url[1]);
            call_user_func_array([$controller, $action], $url);
        }else{
            http_response_code(404);
        }
    }else{
        http_response_code(404);
    }
}else{
    require_once(ROOT.'controllers/Articles.php');
    $controller = new Articles();
    $controller->index();
}


类控制器

<?php

abstract class Controller{
    public function loadModel(string $model){
        require_once ROOT.'models/'.$model.'.php';
        $this->Article
        $model = new $model();
        return $model;
    }

    public function render(string $file, $data= array()){
        extract($data);
        ob_start();
        require_once(ROOT.'views/'.strtolower(get_class($this)).'/'.$file.'.php');
        $content = ob_get_clean();
        require_once(ROOT.'views/layout/base.php');
    }
} 

在我的 base.php 模板上

 <link rel="stylesheet" type="text/css" href="css/style.css">

编辑 我终于成功添加到我的根文件

define('SCRIPT_ROOT', 'http://localhost/EdelweissMagazine');

<link rel="stylesheet" type="text/css" href="<?php echo SCRIPT_ROOT.'/css/style.css';?>">

【问题讨论】:

  • 如何设置绝对路径?

标签: php html css model-view-controller routes


【解决方案1】:

试试这个:

<link href="{{ asset('/css/style.css') }}" rel="stylesheet">

【讨论】:

  • 我不明白它应该如何工作,它不起作用..
猜你喜欢
  • 2019-12-05
  • 2023-02-04
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
  • 2016-02-28
相关资源
最近更新 更多