首页 > 解决方案 > 指向外部样式表的链接在 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'; ?>

标签: php

解决方案


推荐阅读