PHP需要从根文件夹链接

时间:2014-07-01 10:40:14

标签: php

我一直在寻找,实际上我找到了一些相关的主题,但那些建议对我没用。

我的项目文件夹结构如下:

projectSample (name of folder)

 » backend (folder)
  -> index.php
  -> orders.php
  » Admin (folder)
   -> index.php

 » libs (folder)
  -> config.php
  -> orders.php
  -> global.php

 » views (folder)
  » backend (folder)
   -> header.php
   » content (folder)
    -> index.php
    -> orders.php
    -> unique_order.php
    -> add_order.php
    » admin (folder)
     -> index.php

 » css (folder)
 » js (folder)

我在名为getLanguageFile()的文件 global.php 中有一个函数,它需要适当的文件。

public static function getLanguageFile(){
    $default = 'pt';

    if(isset($_COOKIE['language'])){
        switch($_COOKIE['language']){
            case 'fr': break;
            case 'en_US': $default = 'en'; break;
            case 'es': break;
        }
    }
    $path = rootPath . "/languages/" . $default . ".php";

    return require_once($path);
}

我在Backend文件夹的所有文件中调用此函数。变量rootPath config.php 文件中具有以下值。

define("rootPath", '/projectSample');

这不起作用,我在每个页面都收到以下错误:

  

警告:require_once(/projectSample/languages/pt.php):无法打开   stream:没有这样的文件或目录   第370行的C:\ xampp \ htdocs \ projectSample \ libs \ global.php

但是看看路径,它实际上是正确的(/projectSample/languages/pt.php)确实存在。 我试图根据我的需要调整其他功能,例如:

define("rootPath", dirname(dirname(__FILE__))); 

这个似乎适用于require_onces函数,但不适用于css / js文件。 如果我在css / js文件中使用相同的变量来包含我在Chrome控制台上接收和错误:

  

不允许加载本地资源:   文件:/// C:/xampp/htdocs/projectSample/css/bootstrap/bootstrap.min.css

因此,仅使用css / js的另一个变量似乎是最好的方法,但我不能把它用于所有文件。

define("clientPath", dirname(dirname($_SERVER['PHP_SELF'])));

此变量适用于»后端文件夹(以及所有每个文件),但是,它在文件夹Admin上无法正常工作。

<link rel='stylesheet' href='<?php echo clientPath. "/css/bootstrap/bootstrap.min.css"; ?>'/>

» backend (folder) > works
 -> index.php > works
 -> orders.php -> works
 » admin (folder) > does not work
  -> index.php > does not work

它在该文件夹中不起作用的原因是因为最终链接是:

http://localhost/projectSample/backend/css/bootstrap/bootstrap.min.css

它应该是:

http://localhost/projectSample/css/bootstrap/bootstrap.min.css

好的,我可以创建另一个变量(如下所示),但这是最好的方法吗?

define("clientPath_admin", dirname(dirname(dirname($_SERVER['PHP_SELF']))));

很抱歉这个长话题,我没有使用basehref

1 个答案:

答案 0 :(得分:1)

一种可能的方式是,

声明,这是前端路径

 define("BASEPATH", "http://localhost/projectSample/");
 define("BASEPATH_TO_ADMIN", "http://localhost/projectSample/backend/");

在Html部分

<link rel='stylesheet' href='<?php echo BASEPATH. "css/bootstrap/bootstrap.min.css"; ?>'/>
相关问题