Php __前端_uuu常数工作不正常

Php __前端_uuu常数工作不正常,php,constants,Php,Constants,我有以下目录结构: /webroot /static /img /css - style.css /js /pdf /mp3 /mp4 - index.php 在我的'index.php'中,我有以下代码: // define the frontend path constant $frontend = realpath(dirname(__FILE__)); d

我有以下目录结构:

/webroot
    /static
        /img
        /css
          - style.css
        /js
        /pdf
        /mp3
        /mp4
    - index.php
在我的
'index.php'
中,我有以下代码:

// define the frontend path constant
$frontend = realpath(dirname(__FILE__));
define('__FRONTEND__', $frontend);
我的模板位于后端(webroot外部),当我编写类似这样的代码时:

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

为什么我的
“前端”
常量失败?

您不应该在模板中使用
realpath(dirname(\uu文件))
。这是系统的路径,而不是用户可以访问的路径

如果您的web根是webroot/如您所说,那么
href=“/static/css/style.css”
就可以了


此外,您的问题与斜杠和反斜杠无关,它们都在Windows中工作

你看到手册了吗?在Windows上,它将保留反斜杠,只删除
。/
或..\parts。您的
\uuuuu后端\uuuuuuu>常量与另一个有何不同?您期望得到什么?
'href=“/static/css/style.css”
在我的情况下不起作用,因为我有
'
的默认模板。这会在处理不同的URL时产生问题:
www.example.com
www.example.com/items/audio
。我想我使用了
'http://'$_服务器['HTTP_HOST']
作为我的
“前端”
常量如果您的网站托管在子文件夹(如example.com/site/)上,只需将
“前端”
设置到该文件夹:
定义(“前端”、“/site”)
<link type="text/css" 
      rel="stylesheet" 
      href="C:\xampp\htdocs\projectx\webroot/static/css/style.css"
/>
// define the frontend path constant
$backend = realpath(dirname(__FILE__) . '/../backend');
define('__BACKEND__', $backend);