0

I've searching around and I actually found some topics related, but the suggestions there did not worked for me.

My project folder structure is the following:

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)

I have a function inside the file global.php called getLanguageFile(), which requires the file appropriate.

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);
}

I call this function in all files of Backend folder. The variable rootPath has the following value in the config.php file.

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

This does not work and I receive in every page the following error:

Warning: require_once(/projectSample/languages/pt.php): failed to open stream: No such file or directory in C:\xampp\htdocs\projectSample\libs\global.php on line 370

But looking at the path, it is actually correct (/projectSample/languages/pt.php) actually exists. I've tried to adapt other functions to my needs such as:

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

This one seems to work fine for the require_onces function, but not for css/js files. If I use the same variable for css/js files to include I receive and error on Chrome console:

Not allowed to load local resource: file:///C:/xampp/htdocs/projectSample/css/bootstrap/bootstrap.min.css

So, using another variable just for css/js seems the best approach but I can't put it worked for all files.

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

This variable works fine for » Backend folder (and for all each files), however, it does not work well on the folder 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

The reason why it does not work in that folders its because the final link is:

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

And it should be:

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

Ok, I could create another variable (like the below), but is that the best approach?

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

Sorry for the long topic, and I'm not using basehref.

Linesofcode
  • 5,327
  • 13
  • 62
  • 116
  • 1
    Just use `require(_once) __DIR__ . '/relative/path/to/current/file/from/current/directory/calling/file/is/in.php';` – PeeHaa Jul 01 '14 at 10:43
  • 1
    @PeeHaa nice to see i'm not the only one using `__DIR__` which is a stallion. – r3wt Jul 01 '14 at 10:46
  • @PeeHaa I did not get it. If I use (for example) in the file `View->Backend->Content->Admin->index.php` `require_once(__DIR__ . '/libs/orders.php')` I get an error. `Warning: require_once(C:\xampp\htdocs\projectsample\views\backend\content\admin/libs/orders.php): failed to open stream: No such file or directory` – Linesofcode Jul 01 '14 at 10:51
  • `projectsample` vs `projectSample`? – PeeHaa Jul 01 '14 at 10:53
  • Also regarding loading your html resources: http://stackoverflow.com/questions/2005079/absolute-vs-relative-urls/21828923#21828923 – PeeHaa Jul 01 '14 at 10:55
  • Well while copying the error I forgot to change to `projectSample`, it's not that the problem. – Linesofcode Jul 01 '14 at 10:55

1 Answers1

2

One possible way is,

Declare, this is the frontend path

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

In Html part

<link rel='stylesheet' href='<?php echo BASEPATH. "css/bootstrap/bootstrap.min.css"; ?>'/>
Vigneshwaran
  • 387
  • 1
  • 2
  • 14