php - 指向外部样式表的链接在 php 中未按预期工作包括
问题描述
所以我创建了这个全局 header.php 以在我的项目中使用,当我从项目中的另一个目录调用时,指向我的样式表文件夹的链接没有按预期工作。而不是 r_url() 返回 root_folder/stylesheet/..... 它返回 root_folder/public/index.ph/stylesheet/......
但是当我运行 header.php 文件本身时,代码工作正常, r_url() 函数按预期返回 root_folder/stylesheet/...... 因为样式表文件夹位于 root_folder 中,而 r_url() 应该是只返回没有任何 /public 或其他内容的 root_folder。
请帮忙。
谢谢你。
//this is header.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="<?php echo r_url('/stylesheets/bootstrap-4.2.1-dist/css/bootstrap.css');?>">
<title><?php echo $page_title ?></title>
<?php
//This is initialize.php
//Assign file path to PHP constants
//__FILE__ returns the current path to this file
//direname returns the path to the parent directory
define("PRIVATE_PATH", dirname(__FILE__));
define("PROJECT_PATH", dirname(PRIVATE_PATH));
define("PUBLIC_PATH", PROJECT_PATH . '/public');
define("SHARED_PATH", PRIVATE_PATH . '/shared');
//this function is in another separate file
//included here with "require_once"
function r_url($url){
if($url[0] != '/'){
$url = '/' . $url;
}
return WWW_ROOT . $url;
}
//create a constant to the project root directory
$public_end = strpos($_SERVER['SCRIPT_NAME'], '/public') + 16;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $public_end);
define('WWW_ROOT', $doc_root);
?>
///this is the index.php in my public folder that's giving me the problem
<?php
require_once '../../private/initialize.php';
$page_title = 'Home';
include SHARED_PATH . '/header.php'; ?>
解决方案
推荐阅读
- python - ImportError:没有名为 twitter 的模块
- node.js - Mongo DB mongoose schema.Save 函数正在从数据库中删除字段
- javascript - 我无法存储中间数组
- linux - 连接 Postgres Db 时出错:服务器证书与主机名不匹配
- docker - Docker RUN 与 keytool 导入到 Java 信任库成功但在映像构建期间同时失败?
- android - 撰写 LazyList 部分背景
- node.js - 节点 CLI 脚本提前退出
- docker - 如何将 docker 文件系统提取到我的本地文件系统中,这样我就不再需要使用 docker
- python - 我们可以将 mlflow 工件存储在 Azure blob 存储中,以便在 pycaret 中进行新实验吗?
- reactjs - 如何使用 Fluent UI 和 React JS 将自定义样式应用于选择组的选项